
<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
        <link rel="prev" href="../05_JSP%E7%9A%84%E9%9A%90%E5%90%AB%E5%AF%B9%E8%B1%A1%E5%AE%9E%E9%AA%8C/index.html">
      
      
        <link rel="next" href="../07_SpringBoot%E7%BB%BC%E5%90%88%E5%AE%9E%E9%AA%8C/index.html">
      
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.6.1, mkdocs-material-9.6.5">
    
    
      
        <title>06 Servlet及Filter基础实验 - 基于WEB的应用程序开发-实验指导书</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.8608ea7d.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.06af60db.min.css">
      
      


    
    
      
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/extra.css">
    
    <script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="purple" data-md-color-accent="indigo">
  
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#servletfilter" class="md-skip">
          Skip to content
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

  

<header class="md-header md-header--shadow" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../../index.html" title="基于WEB的应用程序开发-实验指导书" class="md-header__button md-logo" aria-label="基于WEB的应用程序开发-实验指导书" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            基于WEB的应用程序开发-实验指导书
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              06 Servlet及Filter基础实验
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
  
    
    
    
    <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="purple" data-md-color-accent="indigo"  aria-label="夜间模式"  type="radio" name="__palette" id="__palette_0">
    
      <label class="md-header__button md-icon" title="夜间模式" for="__palette_1" hidden>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5s-1.65.15-2.39.42zM3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29zm.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14zM20.65 7l-1.77 3.79a7.02 7.02 0 0 0-2.38-4.15zm-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29zM12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44z"/></svg>
      </label>
    
  
    
    
    
    <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="teal" data-md-color-accent="indigo"  aria-label="日间模式"  type="radio" name="__palette" id="__palette_1">
    
      <label class="md-header__button md-icon" title="日间模式" for="__palette_0" hidden>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3zm3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95zm-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31"/></svg>
      </label>
    
  
</form>
      
    
    
      <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script>
    
    
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    



<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../../index.html" title="基于WEB的应用程序开发-实验指导书" class="md-nav__button md-logo" aria-label="基于WEB的应用程序开发-实验指导书" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg>

    </a>
    基于WEB的应用程序开发-实验指导书
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    首页
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    
    
    
      
        
        
      
    
    
    <li class="md-nav__item md-nav__item--section md-nav__item--nested">
      
        
        
          
        
        <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2" >
        
          
          <label class="md-nav__link" for="__nav_2" id="__nav_2_label" tabindex="">
            
  
  <span class="md-ellipsis">
    1. 实验环境搭建
    
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_2">
            <span class="md-nav__icon md-icon"></span>
            1. 实验环境搭建
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../../00_%E7%8E%AF%E5%A2%83%E5%BB%BA%E7%AB%8B/Java/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    1.1 前端环境搭建
    
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../05_%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    1.2 后端境搭建
    
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../01_Html%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    2 Html基础实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../02_CSS%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    3 CSS基础实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../03_Javascript%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    04 Javascript基础
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../03_Javascript%E9%AB%98%E7%BA%A7%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    05 Javascript高级实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../04_jQuery/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    06 jQuery
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../04_Bootstrap/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    06 Bootstrap
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../../04_Vue.js%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    06 Vue.js
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../05_JSP%E7%9A%84%E9%9A%90%E5%90%AB%E5%AF%B9%E8%B1%A1%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    05 JSP的隐含对象实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
    
  
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          
  
  <span class="md-ellipsis">
    06 Servlet及Filter基础实验
    
  </span>
  

          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="index.html" class="md-nav__link md-nav__link--active">
        
  
  <span class="md-ellipsis">
    06 Servlet及Filter基础实验
    
  </span>
  

      </a>
      
        

  

<nav class="md-nav md-nav--secondary" aria-label=" ">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
       
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    <span class="md-ellipsis">
      1 实验类型
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    <span class="md-ellipsis">
      2 实验目的
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#3" class="md-nav__link">
    <span class="md-ellipsis">
      3 实验要求
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#4" class="md-nav__link">
    <span class="md-ellipsis">
      4 实验环境
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#5" class="md-nav__link">
    <span class="md-ellipsis">
      5 实验步骤
    </span>
  </a>
  
    <nav class="md-nav" aria-label="5 实验步骤">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    <span class="md-ellipsis">
      配置文件方式
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    <span class="md-ellipsis">
      注解方式
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    <span class="md-ellipsis">
      处理表单数据
    </span>
  </a>
  
    <nav class="md-nav" aria-label="处理表单数据">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#get" class="md-nav__link">
    <span class="md-ellipsis">
      GET方式
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#post" class="md-nav__link">
    <span class="md-ellipsis">
      POST方式
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#session" class="md-nav__link">
    <span class="md-ellipsis">
      处理Session信息
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    <span class="md-ellipsis">
      综合
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    <span class="md-ellipsis">
      拓展练习
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../07_SpringBoot%E7%BB%BC%E5%90%88%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    07 Spring Boot综合实验
    
  </span>
  

      </a>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  

<nav class="md-nav md-nav--secondary" aria-label=" ">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
       
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    <span class="md-ellipsis">
      1 实验类型
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    <span class="md-ellipsis">
      2 实验目的
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#3" class="md-nav__link">
    <span class="md-ellipsis">
      3 实验要求
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#4" class="md-nav__link">
    <span class="md-ellipsis">
      4 实验环境
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#5" class="md-nav__link">
    <span class="md-ellipsis">
      5 实验步骤
    </span>
  </a>
  
    <nav class="md-nav" aria-label="5 实验步骤">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    <span class="md-ellipsis">
      配置文件方式
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    <span class="md-ellipsis">
      注解方式
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    <span class="md-ellipsis">
      处理表单数据
    </span>
  </a>
  
    <nav class="md-nav" aria-label="处理表单数据">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#get" class="md-nav__link">
    <span class="md-ellipsis">
      GET方式
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#post" class="md-nav__link">
    <span class="md-ellipsis">
      POST方式
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#session" class="md-nav__link">
    <span class="md-ellipsis">
      处理Session信息
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    <span class="md-ellipsis">
      综合
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    <span class="md-ellipsis">
      拓展练习
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  



<h1 id="servletfilter">Servlet及Filter基础实验</h1>
<h2 id="1">1 实验类型</h2>
<p>验证型，2学时，必选实验</p>
<h2 id="2">2 实验目的</h2>
<p>掌握Servlet程序的编写方法；理解前后端数据传输原理；</p>
<h2 id="3">3 实验要求</h2>
<p>验证参考代码；修改参考代码，实现自己的设计；将关键结果截图及源代码整理成实验报告</p>
<h2 id="4">4 实验环境</h2>
<p>Windows 7 64、Open JDK 23+、Tomcat 11+、Spring Tools 4(STS4)、Visual Studio Code(VS Code)、Microsoft Edge/Chrome/Firefox等浏览器
、sqlite-jdbc-3.41.0.0.jar</p>
<h2 id="5">5 实验步骤</h2>
<p>通过两种不同方式（配置文件方式、注解方式）创建Servlet，步骤如下：</p>
<h3 id="_1">配置文件方式</h3>
<ol>
<li>
<p>创建STS4工作目录：<code>{盘符}:/{学号}/exp7</code></p>
<blockquote>
<p>后续文件均存放在工作目录</p>
</blockquote>
</li>
<li>
<p>创建的<strong>Dynamic Web</strong>工程：<strong>HelloServlet</strong></p>
<blockquote>
<p>注意：勾选<code>Generate web.xml deployment descriptor</code></p>
</blockquote>
</li>
<li>
<p>增加<strong>Tomcat</strong>容器；执行<code>Window-&gt;Preferences/Server/Runtime Enviroments/Add</code>，增加Tomcat容器</p>
</li>
<li>
<p>增加<strong>Servlet</strong>相关包；执行<code>Build Path-&gt;Configure Build path-&gt;Libraries-&gt;Add Library-&gt;Server Runtime</code>，增加<code>Server Runtime[Apache Tomcat v9.0]</code></p>
<blockquote>
<p>注意：缺少本步骤则无法访问Servlet</p>
</blockquote>
</li>
<li>
<p>注册Tomcat；在Servers中注册本机Tomcat</p>
</li>
<li>
<p>创建包<strong>Hello</strong>；在src节点下创建包<strong>Hello</strong></p>
</li>
<li>
<p>创建Servlet类<strong>Hello.Hello1</strong>，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">HelloServlet/src/main/java/Hello/Hello1.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-0-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-0-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-0-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-0-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-0-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-0-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-0-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-0-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-0-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-0-10">10</a></span>
<span class="normal"><a href="#__codelineno-0-11">11</a></span>
<span class="normal"><a href="#__codelineno-0-12">12</a></span>
<span class="normal"><a href="#__codelineno-0-13">13</a></span>
<span class="normal"><a href="#__codelineno-0-14">14</a></span>
<span class="normal"><a href="#__codelineno-0-15">15</a></span>
<span class="normal"><a href="#__codelineno-0-16">16</a></span>
<span class="normal"><a href="#__codelineno-0-17">17</a></span>
<span class="normal"><a href="#__codelineno-0-18">18</a></span>
<span class="normal"><a href="#__codelineno-0-19">19</a></span>
<span class="normal"><a href="#__codelineno-0-20">20</a></span>
<span class="normal"><a href="#__codelineno-0-21">21</a></span>
<span class="normal"><a href="#__codelineno-0-22">22</a></span>
<span class="normal"><a href="#__codelineno-0-23">23</a></span>
<span class="normal"><a href="#__codelineno-0-24">24</a></span>
<span class="normal"><a href="#__codelineno-0-25">25</a></span>
<span class="normal"><a href="#__codelineno-0-26">26</a></span>
<span class="normal"><a href="#__codelineno-0-27">27</a></span>
<span class="normal"><a href="#__codelineno-0-28">28</a></span>
<span class="normal"><a href="#__codelineno-0-29">29</a></span>
<span class="normal"><a href="#__codelineno-0-30">30</a></span>
<span class="normal"><a href="#__codelineno-0-31">31</a></span>
<span class="normal"><a href="#__codelineno-0-32">32</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">Hello</span><span class="p">;</span>
<a id="__codelineno-0-2" name="__codelineno-0-2"></a>
<a id="__codelineno-0-3" name="__codelineno-0-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.io.IOException</span><span class="p">;</span>
<a id="__codelineno-0-4" name="__codelineno-0-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletConfig</span><span class="p">;</span>
<a id="__codelineno-0-5" name="__codelineno-0-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletException</span><span class="p">;</span>
<a id="__codelineno-0-6" name="__codelineno-0-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServlet</span><span class="p">;</span>
<a id="__codelineno-0-7" name="__codelineno-0-7"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletRequest</span><span class="p">;</span>
<a id="__codelineno-0-8" name="__codelineno-0-8"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletResponse</span><span class="p">;</span>
<a id="__codelineno-0-9" name="__codelineno-0-9"></a>
<a id="__codelineno-0-10" name="__codelineno-0-10"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Hello1</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">HttpServlet</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-11" name="__codelineno-0-11"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">serialVersionUID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1L</span><span class="p">;</span>
<a id="__codelineno-0-12" name="__codelineno-0-12"></a>
<a id="__codelineno-0-13" name="__codelineno-0-13"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">message</span><span class="p">;</span>
<a id="__codelineno-0-14" name="__codelineno-0-14"></a>
<a id="__codelineno-0-15" name="__codelineno-0-15"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="nf">Hello1</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-16" name="__codelineno-0-16"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-0-17" name="__codelineno-0-17"></a>
<a id="__codelineno-0-18" name="__codelineno-0-18"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">init</span><span class="p">(</span><span class="n">ServletConfig</span><span class="w"> </span><span class="n">config</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-19" name="__codelineno-0-19"></a><span class="w">        </span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;Hello World!&quot;</span><span class="p">;</span>
<a id="__codelineno-0-20" name="__codelineno-0-20"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-0-21" name="__codelineno-0-21"></a>
<a id="__codelineno-0-22" name="__codelineno-0-22"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doGet</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span>
<a id="__codelineno-0-23" name="__codelineno-0-23"></a><span class="w">            </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-24" name="__codelineno-0-24"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">setHeader</span><span class="p">(</span><span class="s">&quot;content-type&quot;</span><span class="p">,</span><span class="s">&quot;text/html;charset=utf-8&quot;</span><span class="p">);</span>
<a id="__codelineno-0-25" name="__codelineno-0-25"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">getWriter</span><span class="p">().</span><span class="na">append</span><span class="p">(</span><span class="s">&quot;&lt;h1&gt;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;&lt;/h1&gt;&quot;</span><span class="p">);</span>
<a id="__codelineno-0-26" name="__codelineno-0-26"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-0-27" name="__codelineno-0-27"></a>
<a id="__codelineno-0-28" name="__codelineno-0-28"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doPost</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-29" name="__codelineno-0-29"></a><span class="w">        </span><span class="n">doGet</span><span class="p">(</span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">response</span><span class="p">);</span>
<a id="__codelineno-0-30" name="__codelineno-0-30"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-0-31" name="__codelineno-0-31"></a>
<a id="__codelineno-0-32" name="__codelineno-0-32"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>在<code>web.xml</code>中注册<strong>Hello1</strong>，参考配置如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">HelloServlet/src/main/webapp/WEB-INF/web.xml</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-1-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-1-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-1-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-1-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-1-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-1-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-1-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-1-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-1-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-1-10">10</a></span>
<span class="normal"><a href="#__codelineno-1-11">11</a></span>
<span class="normal"><a href="#__codelineno-1-12">12</a></span>
<span class="normal"><a href="#__codelineno-1-13">13</a></span>
<span class="normal"><a href="#__codelineno-1-14">14</a></span>
<span class="normal"><a href="#__codelineno-1-15">15</a></span>
<span class="normal"><a href="#__codelineno-1-16">16</a></span>
<span class="normal"><a href="#__codelineno-1-17">17</a></span>
<span class="normal"><a href="#__codelineno-1-18">18</a></span>
<span class="normal"><a href="#__codelineno-1-19">19</a></span>
<span class="normal"><a href="#__codelineno-1-20">20</a></span>
<span class="normal"><a href="#__codelineno-1-21">21</a></span>
<span class="normal"><a href="#__codelineno-1-22">22</a></span>
<span class="normal"><a href="#__codelineno-1-23">23</a></span>
<span class="normal"><a href="#__codelineno-1-24">24</a></span>
<span class="normal"><a href="#__codelineno-1-25">25</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1"></a><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<a id="__codelineno-1-2" name="__codelineno-1-2"></a><span class="nt">&lt;web-app</span><span class="w"> </span><span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<a id="__codelineno-1-3" name="__codelineno-1-3"></a><span class="w">    </span><span class="na">xmlns=</span><span class="s">&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;</span>
<a id="__codelineno-1-4" name="__codelineno-1-4"></a><span class="w">    </span><span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;</span>
<a id="__codelineno-1-5" name="__codelineno-1-5"></a><span class="w">    </span><span class="na">id=</span><span class="s">&quot;WebApp_ID&quot;</span><span class="w"> </span><span class="na">version=</span><span class="s">&quot;4.0&quot;</span><span class="nt">&gt;</span>
<a id="__codelineno-1-6" name="__codelineno-1-6"></a><span class="w">    </span><span class="nt">&lt;display-name&gt;</span>HelloServlet<span class="nt">&lt;/display-name&gt;</span>
<a id="__codelineno-1-7" name="__codelineno-1-7"></a><span class="w">    </span><span class="nt">&lt;welcome-file-list&gt;</span>
<a id="__codelineno-1-8" name="__codelineno-1-8"></a><span class="w">        </span><span class="nt">&lt;welcome-file&gt;</span>index.html<span class="nt">&lt;/welcome-file&gt;</span>
<a id="__codelineno-1-9" name="__codelineno-1-9"></a><span class="w">        </span><span class="nt">&lt;welcome-file&gt;</span>index.htm<span class="nt">&lt;/welcome-file&gt;</span>
<a id="__codelineno-1-10" name="__codelineno-1-10"></a><span class="w">        </span><span class="nt">&lt;welcome-file&gt;</span>index.jsp<span class="nt">&lt;/welcome-file&gt;</span>
<a id="__codelineno-1-11" name="__codelineno-1-11"></a><span class="w">        </span><span class="nt">&lt;welcome-file&gt;</span>default.html<span class="nt">&lt;/welcome-file&gt;</span>
<a id="__codelineno-1-12" name="__codelineno-1-12"></a><span class="w">        </span><span class="nt">&lt;welcome-file&gt;</span>default.htm<span class="nt">&lt;/welcome-file&gt;</span>
<a id="__codelineno-1-13" name="__codelineno-1-13"></a><span class="w">        </span><span class="nt">&lt;welcome-file&gt;</span>default.jsp<span class="nt">&lt;/welcome-file&gt;</span>
<a id="__codelineno-1-14" name="__codelineno-1-14"></a><span class="w">    </span><span class="nt">&lt;/welcome-file-list&gt;</span>
<a id="__codelineno-1-15" name="__codelineno-1-15"></a>
<a id="__codelineno-1-16" name="__codelineno-1-16"></a><span class="w">    </span><span class="nt">&lt;servlet&gt;</span>
<a id="__codelineno-1-17" name="__codelineno-1-17"></a><span class="w">        </span><span class="nt">&lt;servlet-name&gt;</span>Hello1<span class="nt">&lt;/servlet-name&gt;</span>
<a id="__codelineno-1-18" name="__codelineno-1-18"></a><span class="w">        </span><span class="nt">&lt;servlet-class&gt;</span>Hello.Hello1<span class="nt">&lt;/servlet-class&gt;</span>
<a id="__codelineno-1-19" name="__codelineno-1-19"></a><span class="w">    </span><span class="nt">&lt;/servlet&gt;</span>
<a id="__codelineno-1-20" name="__codelineno-1-20"></a>
<a id="__codelineno-1-21" name="__codelineno-1-21"></a><span class="w">    </span><span class="nt">&lt;servlet-mapping&gt;</span>
<a id="__codelineno-1-22" name="__codelineno-1-22"></a><span class="w">        </span><span class="nt">&lt;servlet-name&gt;</span>Hello1<span class="nt">&lt;/servlet-name&gt;</span>
<a id="__codelineno-1-23" name="__codelineno-1-23"></a><span class="w">        </span><span class="nt">&lt;url-pattern&gt;</span>/Hello1<span class="nt">&lt;/url-pattern&gt;</span>
<a id="__codelineno-1-24" name="__codelineno-1-24"></a><span class="w">    </span><span class="nt">&lt;/servlet-mapping&gt;</span>
<a id="__codelineno-1-25" name="__codelineno-1-25"></a><span class="nt">&lt;/web-app&gt;</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>部署工程到Tomat并启动</p>
</li>
<li>
<p>在浏览器中访问<code>http://localhost:8080/HelloServlet/Hello1</code>并观察结果是否正确</p>
<!--
    ??? tip "运行结果"
        <iframe src="./src/HelloServletResult/Hello1.html" frameborder="0" width="100%" height="80"></iframe>
-->
</li>
</ol>
<h3 id="_2">注解方式</h3>
<ol>
<li>
<p>在HelloServlet工程中创建类<code>Hello.Hello2</code>，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">HelloServlet/src/main/java/Hello/Hello2.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-2-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-2-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-2-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-2-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-2-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-2-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-2-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-2-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-2-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-2-10">10</a></span>
<span class="normal"><a href="#__codelineno-2-11">11</a></span>
<span class="normal"><a href="#__codelineno-2-12">12</a></span>
<span class="normal"><a href="#__codelineno-2-13">13</a></span>
<span class="normal"><a href="#__codelineno-2-14">14</a></span>
<span class="normal"><a href="#__codelineno-2-15">15</a></span>
<span class="normal"><a href="#__codelineno-2-16">16</a></span>
<span class="normal"><a href="#__codelineno-2-17">17</a></span>
<span class="normal"><a href="#__codelineno-2-18">18</a></span>
<span class="normal"><a href="#__codelineno-2-19">19</a></span>
<span class="normal"><a href="#__codelineno-2-20">20</a></span>
<span class="normal"><a href="#__codelineno-2-21">21</a></span>
<span class="normal"><a href="#__codelineno-2-22">22</a></span>
<span class="normal"><a href="#__codelineno-2-23">23</a></span>
<span class="normal"><a href="#__codelineno-2-24">24</a></span>
<span class="normal"><a href="#__codelineno-2-25">25</a></span>
<span class="normal"><a href="#__codelineno-2-26">26</a></span>
<span class="normal"><a href="#__codelineno-2-27">27</a></span>
<span class="normal"><a href="#__codelineno-2-28">28</a></span>
<span class="normal"><a href="#__codelineno-2-29">29</a></span>
<span class="normal"><a href="#__codelineno-2-30">30</a></span>
<span class="normal"><a href="#__codelineno-2-31">31</a></span>
<span class="normal"><a href="#__codelineno-2-32">32</a></span>
<span class="normal"><a href="#__codelineno-2-33">33</a></span>
<span class="normal"><a href="#__codelineno-2-34">34</a></span>
<span class="normal"><a href="#__codelineno-2-35">35</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">Hello</span><span class="p">;</span>
<a id="__codelineno-2-2" name="__codelineno-2-2"></a>
<a id="__codelineno-2-3" name="__codelineno-2-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.io.IOException</span><span class="p">;</span>
<a id="__codelineno-2-4" name="__codelineno-2-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletConfig</span><span class="p">;</span>
<a id="__codelineno-2-5" name="__codelineno-2-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletException</span><span class="p">;</span>
<a id="__codelineno-2-6" name="__codelineno-2-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.annotation.WebServlet</span><span class="p">;</span>
<a id="__codelineno-2-7" name="__codelineno-2-7"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServlet</span><span class="p">;</span>
<a id="__codelineno-2-8" name="__codelineno-2-8"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletRequest</span><span class="p">;</span>
<a id="__codelineno-2-9" name="__codelineno-2-9"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletResponse</span><span class="p">;</span>
<a id="__codelineno-2-10" name="__codelineno-2-10"></a>
<a id="__codelineno-2-11" name="__codelineno-2-11"></a><span class="nd">@WebServlet</span><span class="p">(</span><span class="s">&quot;/Hello2&quot;</span><span class="p">)</span>
<a id="__codelineno-2-12" name="__codelineno-2-12"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Hello2</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">HttpServlet</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-2-13" name="__codelineno-2-13"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">serialVersionUID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1L</span><span class="p">;</span>
<a id="__codelineno-2-14" name="__codelineno-2-14"></a>
<a id="__codelineno-2-15" name="__codelineno-2-15"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">message</span><span class="p">;</span>
<a id="__codelineno-2-16" name="__codelineno-2-16"></a>
<a id="__codelineno-2-17" name="__codelineno-2-17"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="nf">Hello2</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-2-18" name="__codelineno-2-18"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-2-19" name="__codelineno-2-19"></a>
<a id="__codelineno-2-20" name="__codelineno-2-20"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">init</span><span class="p">(</span><span class="n">ServletConfig</span><span class="w"> </span><span class="n">config</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-2-21" name="__codelineno-2-21"></a><span class="w">        </span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;Hello World Again!&quot;</span><span class="p">;</span>
<a id="__codelineno-2-22" name="__codelineno-2-22"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-2-23" name="__codelineno-2-23"></a>
<a id="__codelineno-2-24" name="__codelineno-2-24"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doGet</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span>
<a id="__codelineno-2-25" name="__codelineno-2-25"></a><span class="w">            </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-2-26" name="__codelineno-2-26"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">setHeader</span><span class="p">(</span><span class="s">&quot;content-type&quot;</span><span class="p">,</span><span class="s">&quot;text/html;charset=utf-8&quot;</span><span class="p">);</span>
<a id="__codelineno-2-27" name="__codelineno-2-27"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">getWriter</span><span class="p">().</span><span class="na">append</span><span class="p">(</span><span class="s">&quot;&lt;h1&gt;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;&lt;/h1&gt;&quot;</span><span class="p">);</span>
<a id="__codelineno-2-28" name="__codelineno-2-28"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-2-29" name="__codelineno-2-29"></a>
<a id="__codelineno-2-30" name="__codelineno-2-30"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doPost</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span>
<a id="__codelineno-2-31" name="__codelineno-2-31"></a><span class="w">            </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-2-32" name="__codelineno-2-32"></a><span class="w">        </span><span class="n">doGet</span><span class="p">(</span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">response</span><span class="p">);</span>
<a id="__codelineno-2-33" name="__codelineno-2-33"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-2-34" name="__codelineno-2-34"></a>
<a id="__codelineno-2-35" name="__codelineno-2-35"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>在浏览器中访问<code>http://localhost:8080/HelloServlet/Hello2</code>并观察结果是否正确</p>
<!--
    ??? tip "运行结果"
        <iframe src="./src/HelloServletResult/Hello2.html" frameborder="0" width="100%" height="80"></iframe>
-->
</li>
</ol>
<h3 id="_3">处理表单数据</h3>
<p>使用Servlet实现加法运算，前端用GET/POST两种方式采集操作数，步骤如下：</p>
<h4 id="get">GET方式</h4>
<ol>
<li>
<p>在HelloServet工程创建页面<code>AddForm.jsp</code>用于收集信息，参考代码下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">HelloServlet/src/main/webapp/AddForm.jsp</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-3-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-3-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-3-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-3-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-3-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-3-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-3-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-3-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-3-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-3-10">10</a></span>
<span class="normal"><a href="#__codelineno-3-11">11</a></span>
<span class="normal"><a href="#__codelineno-3-12">12</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1"></a><span class="k">&lt;%@</span><span class="w"> </span><span class="n">page</span><span class="w"> </span><span class="n">language</span><span class="o">=</span><span class="s">&quot;java&quot;</span><span class="w"> </span><span class="n">contentType</span><span class="o">=</span><span class="s">&quot;text/html; charset=utf-8&quot;</span>
<a id="__codelineno-3-2" name="__codelineno-3-2"></a><span class="w">    </span><span class="n">pageEncoding</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="k">%&gt;</span>
<a id="__codelineno-3-3" name="__codelineno-3-3"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-3-4" name="__codelineno-3-4"></a><span class="nt">&lt;html&gt;</span>
<a id="__codelineno-3-5" name="__codelineno-3-5"></a><span class="nt">&lt;body&gt;</span>
<a id="__codelineno-3-6" name="__codelineno-3-6"></a><span class="w">    </span><span class="nt">&lt;form</span><span class="w"> </span><span class="na">action=</span><span class="s">&quot;add&quot;</span><span class="w"> </span><span class="na">method=</span><span class="s">&quot;GET&quot;</span><span class="nt">&gt;</span>
<a id="__codelineno-3-7" name="__codelineno-3-7"></a><span class="w">        </span>a：<span class="nt">&lt;input</span><span class="w"> </span><span class="na">type=</span><span class="s">&quot;text&quot;</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;a&quot;</span><span class="nt">/&gt;</span><span class="w"> </span><span class="nt">&lt;br/&gt;</span><span class="w"> </span>
<a id="__codelineno-3-8" name="__codelineno-3-8"></a><span class="w">        </span>b：<span class="nt">&lt;input</span><span class="w"> </span><span class="na">type=</span><span class="s">&quot;text&quot;</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;b&quot;</span><span class="w"> </span><span class="nt">/&gt;</span><span class="w">  </span><span class="nt">&lt;br/&gt;</span><span class="w"> </span>
<a id="__codelineno-3-9" name="__codelineno-3-9"></a><span class="w">        </span><span class="nt">&lt;input</span><span class="w"> </span><span class="na">type=</span><span class="s">&quot;submit&quot;</span><span class="w"> </span><span class="na">value=</span><span class="s">&quot;求和&quot;</span><span class="w"> </span><span class="nt">/&gt;</span>
<a id="__codelineno-3-10" name="__codelineno-3-10"></a><span class="w">    </span><span class="nt">&lt;/form&gt;</span>
<a id="__codelineno-3-11" name="__codelineno-3-11"></a><span class="nt">&lt;/body&gt;</span>
<a id="__codelineno-3-12" name="__codelineno-3-12"></a><span class="nt">&lt;/html&gt;</span>
</code></pre></div></td></tr></table></div>
<blockquote>
<p>注意：表单的提交方式为<strong>GET</strong></p>
</blockquote>
</li>
<li>
<p>创建名为<code>Hello.AddServlet</code>的信息处理Servlet类，并在<strong>doGet</strong>方法中处理表单参数，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">HelloServlet/src/main/java/Hello/AddServlet.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-4-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-4-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-4-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-4-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-4-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-4-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-4-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-4-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-4-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-4-10">10</a></span>
<span class="normal"><a href="#__codelineno-4-11">11</a></span>
<span class="normal"><a href="#__codelineno-4-12">12</a></span>
<span class="normal"><a href="#__codelineno-4-13">13</a></span>
<span class="normal"><a href="#__codelineno-4-14">14</a></span>
<span class="normal"><a href="#__codelineno-4-15">15</a></span>
<span class="normal"><a href="#__codelineno-4-16">16</a></span>
<span class="normal"><a href="#__codelineno-4-17">17</a></span>
<span class="normal"><a href="#__codelineno-4-18">18</a></span>
<span class="normal"><a href="#__codelineno-4-19">19</a></span>
<span class="normal"><a href="#__codelineno-4-20">20</a></span>
<span class="normal"><a href="#__codelineno-4-21">21</a></span>
<span class="normal"><a href="#__codelineno-4-22">22</a></span>
<span class="normal"><a href="#__codelineno-4-23">23</a></span>
<span class="normal"><a href="#__codelineno-4-24">24</a></span>
<span class="normal"><a href="#__codelineno-4-25">25</a></span>
<span class="normal"><a href="#__codelineno-4-26">26</a></span>
<span class="normal"><a href="#__codelineno-4-27">27</a></span>
<span class="normal"><a href="#__codelineno-4-28">28</a></span>
<span class="normal"><a href="#__codelineno-4-29">29</a></span>
<span class="normal"><a href="#__codelineno-4-30">30</a></span>
<span class="normal"><a href="#__codelineno-4-31">31</a></span>
<span class="normal"><a href="#__codelineno-4-32">32</a></span>
<span class="normal"><a href="#__codelineno-4-33">33</a></span>
<span class="normal"><a href="#__codelineno-4-34">34</a></span>
<span class="normal"><a href="#__codelineno-4-35">35</a></span>
<span class="normal"><a href="#__codelineno-4-36">36</a></span>
<span class="normal"><a href="#__codelineno-4-37">37</a></span>
<span class="normal"><a href="#__codelineno-4-38">38</a></span>
<span class="normal"><a href="#__codelineno-4-39">39</a></span>
<span class="normal"><a href="#__codelineno-4-40">40</a></span>
<span class="normal"><a href="#__codelineno-4-41">41</a></span>
<span class="normal"><a href="#__codelineno-4-42">42</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">Hello</span><span class="p">;</span>
<a id="__codelineno-4-2" name="__codelineno-4-2"></a>
<a id="__codelineno-4-3" name="__codelineno-4-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.io.IOException</span><span class="p">;</span>
<a id="__codelineno-4-4" name="__codelineno-4-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletConfig</span><span class="p">;</span>
<a id="__codelineno-4-5" name="__codelineno-4-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletException</span><span class="p">;</span>
<a id="__codelineno-4-6" name="__codelineno-4-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.annotation.WebServlet</span><span class="p">;</span>
<a id="__codelineno-4-7" name="__codelineno-4-7"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServlet</span><span class="p">;</span>
<a id="__codelineno-4-8" name="__codelineno-4-8"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletRequest</span><span class="p">;</span>
<a id="__codelineno-4-9" name="__codelineno-4-9"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletResponse</span><span class="p">;</span>
<a id="__codelineno-4-10" name="__codelineno-4-10"></a>
<a id="__codelineno-4-11" name="__codelineno-4-11"></a><span class="nd">@WebServlet</span><span class="p">(</span><span class="s">&quot;/add&quot;</span><span class="p">)</span>
<a id="__codelineno-4-12" name="__codelineno-4-12"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">AddServlet</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">HttpServlet</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-13" name="__codelineno-4-13"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">serialVersionUID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1L</span><span class="p">;</span>
<a id="__codelineno-4-14" name="__codelineno-4-14"></a>
<a id="__codelineno-4-15" name="__codelineno-4-15"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="nf">AddServlet</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-16" name="__codelineno-4-16"></a><span class="w">        </span><span class="kd">super</span><span class="p">();</span>
<a id="__codelineno-4-17" name="__codelineno-4-17"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-4-18" name="__codelineno-4-18"></a>
<a id="__codelineno-4-19" name="__codelineno-4-19"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">init</span><span class="p">(</span><span class="n">ServletConfig</span><span class="w"> </span><span class="n">config</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-20" name="__codelineno-4-20"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-4-21" name="__codelineno-4-21"></a>
<a id="__codelineno-4-22" name="__codelineno-4-22"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doGet</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span>
<a id="__codelineno-4-23" name="__codelineno-4-23"></a><span class="w">            </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-24" name="__codelineno-4-24"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">setHeader</span><span class="p">(</span><span class="s">&quot;content-type&quot;</span><span class="p">,</span><span class="s">&quot;text/html;charset=utf-8&quot;</span><span class="p">);</span>
<a id="__codelineno-4-25" name="__codelineno-4-25"></a><span class="w">        </span><span class="n">String</span><span class="w"> </span><span class="n">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;a+b=&quot;</span><span class="p">;</span>
<a id="__codelineno-4-26" name="__codelineno-4-26"></a><span class="w">        </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-27" name="__codelineno-4-27"></a><span class="w">            </span><span class="n">Float</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Float</span><span class="p">.</span><span class="na">parseFloat</span><span class="p">(</span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;a&quot;</span><span class="p">));</span>
<a id="__codelineno-4-28" name="__codelineno-4-28"></a><span class="w">            </span><span class="n">Float</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Float</span><span class="p">.</span><span class="na">parseFloat</span><span class="p">(</span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;b&quot;</span><span class="p">));</span>
<a id="__codelineno-4-29" name="__codelineno-4-29"></a><span class="w">            </span><span class="n">message</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="p">;</span>
<a id="__codelineno-4-30" name="__codelineno-4-30"></a><span class="w">        </span><span class="p">}</span><span class="k">catch</span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">){</span>
<a id="__codelineno-4-31" name="__codelineno-4-31"></a>
<a id="__codelineno-4-32" name="__codelineno-4-32"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-4-33" name="__codelineno-4-33"></a>
<a id="__codelineno-4-34" name="__codelineno-4-34"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">getWriter</span><span class="p">().</span><span class="na">write</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
<a id="__codelineno-4-35" name="__codelineno-4-35"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-4-36" name="__codelineno-4-36"></a>
<a id="__codelineno-4-37" name="__codelineno-4-37"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doPost</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span>
<a id="__codelineno-4-38" name="__codelineno-4-38"></a><span class="w">            </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-4-39" name="__codelineno-4-39"></a><span class="w">        </span><span class="n">doGet</span><span class="p">(</span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">response</span><span class="p">);</span>
<a id="__codelineno-4-40" name="__codelineno-4-40"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-4-41" name="__codelineno-4-41"></a>
<a id="__codelineno-4-42" name="__codelineno-4-42"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>部署工程并在浏览器中访问<code>http://localhost:8080/HelloServlet/AddForm.jsp</code></p>
<!--
    ??? tip "运行结果"
        <iframe src="./src/HelloServletResult/AddForm.jsp.html" frameborder="0" width="100%" height="100"></iframe>
-->
</li>
<li>
<p>在表单中填写信息并提交，观察页面变化，确认是否为AddServlet返回的结果</p>
<!--
    ??? tip "运行结果"
        <iframe src="./src/HelloServletResult/add.html" frameborder="0" width="100%" height="100"></iframe>
-->
</li>
</ol>
<h4 id="post">POST方式</h4>
<ol>
<li>
<p>将<code>AddForm.jsp</code>中表单的提交方式改为<strong>POST</strong></p>
</li>
<li>
<p>在AddServlet类的<strong>doPost</strong>增加处理表单参数代码，参考上步代码</p>
</li>
<li>
<p>重新访问<code>http://localhost:8080/HelloServlet/AddForm.jsp</code></p>
</li>
</ol>
<h3 id="session">处理Session信息</h3>
<p>编写新的Servlet类访问Session对象的信息并在显示在页面中。Sessioin对象主要方法如下：</p>
<table>
<thead>
<tr>
<th>方法</th>
<th>功能</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>public Object getAttribute(String name)</strong></td>
<td>该方法返回在该 session 会话中具有指定名称的对象，如果没有指定名称的对象，则返回 null。</td>
</tr>
<tr>
<td><strong>public Enumeration getAttributeNames()</strong></td>
<td>该方法返回 String 对象的枚举，String 对象包含所有绑定到该 session 会话的对象的名称。</td>
</tr>
<tr>
<td><strong>public long getCreationTime()</strong></td>
<td>该方法返回该 session 会话被创建的时间，自格林尼治标准时间 1970 年 1 月 1 日午夜算起，以毫秒为单位。</td>
</tr>
<tr>
<td><strong>public String getId()</strong></td>
<td>该方法返回一个包含分配给该 session 会话的唯一标识符的字符串。</td>
</tr>
<tr>
<td><strong>public long getLastAccessedTime()</strong></td>
<td>该方法返回客户端最后一次发送与该 session 会话相关的请求的时间自格林尼治标准时间 1970 年 1 月 1 日午夜算起，以毫秒为单位。</td>
</tr>
<tr>
<td><strong>public int getMaxInactiveInterval()</strong></td>
<td>该方法返回 Servlet 容器在客户端访问时保持 session 会话打开的最大时间间隔，以秒为单位。</td>
</tr>
<tr>
<td><strong>public void invalidate()</strong></td>
<td>该方法指示该 session 会话无效，并解除绑定到它上面的任何对象。</td>
</tr>
<tr>
<td><strong>public boolean isNew()</strong></td>
<td>如果客户端还不知道该 session 会话，或者如果客户选择不参入该 session 会话，则该方法返回 true。</td>
</tr>
<tr>
<td><strong>public void removeAttribute(String name)</strong></td>
<td>该方法将从该 session 会话移除指定名称的对象。</td>
</tr>
<tr>
<td><strong>public void setAttribute(String name, Object value)</strong></td>
<td>该方法使用指定的名称绑定一个对象到该 session 会话。</td>
</tr>
<tr>
<td><strong>public void setMaxInactiveInterval(int interval)</strong></td>
<td>该方法在 Servlet 容器指示该 session 会话无效之前，指定客户端请求之间的时间，以秒为单位。</td>
</tr>
</tbody>
</table>
<p>具体步骤如下：</p>
<ol>
<li>
<p>创建<code>SessionServlet</code>Servlet，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">HelloServlet/src/main/java/Hello/SessionServlet.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-5-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-5-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-5-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-5-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-5-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-5-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-5-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-5-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-5-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-5-10">10</a></span>
<span class="normal"><a href="#__codelineno-5-11">11</a></span>
<span class="normal"><a href="#__codelineno-5-12">12</a></span>
<span class="normal"><a href="#__codelineno-5-13">13</a></span>
<span class="normal"><a href="#__codelineno-5-14">14</a></span>
<span class="normal"><a href="#__codelineno-5-15">15</a></span>
<span class="normal"><a href="#__codelineno-5-16">16</a></span>
<span class="normal"><a href="#__codelineno-5-17">17</a></span>
<span class="normal"><a href="#__codelineno-5-18">18</a></span>
<span class="normal"><a href="#__codelineno-5-19">19</a></span>
<span class="normal"><a href="#__codelineno-5-20">20</a></span>
<span class="normal"><a href="#__codelineno-5-21">21</a></span>
<span class="normal"><a href="#__codelineno-5-22">22</a></span>
<span class="normal"><a href="#__codelineno-5-23">23</a></span>
<span class="normal"><a href="#__codelineno-5-24">24</a></span>
<span class="normal"><a href="#__codelineno-5-25">25</a></span>
<span class="normal"><a href="#__codelineno-5-26">26</a></span>
<span class="normal"><a href="#__codelineno-5-27">27</a></span>
<span class="normal"><a href="#__codelineno-5-28">28</a></span>
<span class="normal"><a href="#__codelineno-5-29">29</a></span>
<span class="normal"><a href="#__codelineno-5-30">30</a></span>
<span class="normal"><a href="#__codelineno-5-31">31</a></span>
<span class="normal"><a href="#__codelineno-5-32">32</a></span>
<span class="normal"><a href="#__codelineno-5-33">33</a></span>
<span class="normal"><a href="#__codelineno-5-34">34</a></span>
<span class="normal"><a href="#__codelineno-5-35">35</a></span>
<span class="normal"><a href="#__codelineno-5-36">36</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">Hello</span><span class="p">;</span>
<a id="__codelineno-5-2" name="__codelineno-5-2"></a>
<a id="__codelineno-5-3" name="__codelineno-5-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.io.IOException</span><span class="p">;</span>
<a id="__codelineno-5-4" name="__codelineno-5-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletException</span><span class="p">;</span>
<a id="__codelineno-5-5" name="__codelineno-5-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.annotation.WebServlet</span><span class="p">;</span>
<a id="__codelineno-5-6" name="__codelineno-5-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServlet</span><span class="p">;</span>
<a id="__codelineno-5-7" name="__codelineno-5-7"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletRequest</span><span class="p">;</span>
<a id="__codelineno-5-8" name="__codelineno-5-8"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletResponse</span><span class="p">;</span>
<a id="__codelineno-5-9" name="__codelineno-5-9"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpSession</span><span class="p">;</span>
<a id="__codelineno-5-10" name="__codelineno-5-10"></a>
<a id="__codelineno-5-11" name="__codelineno-5-11"></a><span class="nd">@WebServlet</span><span class="p">(</span><span class="s">&quot;/getSessionInfo&quot;</span><span class="p">)</span>
<a id="__codelineno-5-12" name="__codelineno-5-12"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">SessionServlet</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">HttpServlet</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-13" name="__codelineno-5-13"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">final</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="n">serialVersionUID</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1L</span><span class="p">;</span>
<a id="__codelineno-5-14" name="__codelineno-5-14"></a>
<a id="__codelineno-5-15" name="__codelineno-5-15"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="nf">SessionServlet</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-16" name="__codelineno-5-16"></a><span class="w">        </span><span class="kd">super</span><span class="p">();</span>
<a id="__codelineno-5-17" name="__codelineno-5-17"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-5-18" name="__codelineno-5-18"></a>
<a id="__codelineno-5-19" name="__codelineno-5-19"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doGet</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-20" name="__codelineno-5-20"></a><span class="w">        </span><span class="n">HttpSession</span><span class="w"> </span><span class="n">session</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">request</span><span class="p">.</span><span class="na">getSession</span><span class="p">();</span>
<a id="__codelineno-5-21" name="__codelineno-5-21"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">setHeader</span><span class="p">(</span><span class="s">&quot;content-type&quot;</span><span class="p">,</span><span class="s">&quot;text/html;charset=utf-8&quot;</span><span class="p">);</span>
<a id="__codelineno-5-22" name="__codelineno-5-22"></a>
<a id="__codelineno-5-23" name="__codelineno-5-23"></a><span class="w">        </span><span class="n">String</span><span class="w"> </span><span class="n">info</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;&lt;h3&gt;Session Infomation&lt;/h3&gt;\n&quot;</span><span class="p">;</span>
<a id="__codelineno-5-24" name="__codelineno-5-24"></a><span class="w">        </span><span class="n">info</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">String</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="s">&quot;&lt;p&gt;Creation Time:%s&lt;/p&gt;\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">session</span><span class="p">.</span><span class="na">getCreationTime</span><span class="p">());</span>
<a id="__codelineno-5-25" name="__codelineno-5-25"></a><span class="w">        </span><span class="n">info</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">String</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="s">&quot;&lt;p&gt;Session Id:%s&lt;/p&gt;\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">session</span><span class="p">.</span><span class="na">getId</span><span class="p">());</span>
<a id="__codelineno-5-26" name="__codelineno-5-26"></a><span class="w">        </span><span class="n">info</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">String</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="s">&quot;&lt;p&gt;Last Accessed Time:%s&lt;/p&gt;\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">session</span><span class="p">.</span><span class="na">getLastAccessedTime</span><span class="p">());</span>
<a id="__codelineno-5-27" name="__codelineno-5-27"></a><span class="w">        </span><span class="n">info</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">String</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="s">&quot;&lt;p&gt;Max Inactive Interval:%s&lt;/p&gt;\n&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">session</span><span class="p">.</span><span class="na">getMaxInactiveInterval</span><span class="p">());</span>
<a id="__codelineno-5-28" name="__codelineno-5-28"></a>
<a id="__codelineno-5-29" name="__codelineno-5-29"></a><span class="w">        </span><span class="n">response</span><span class="p">.</span><span class="na">getWriter</span><span class="p">().</span><span class="na">append</span><span class="p">(</span><span class="n">info</span><span class="p">);</span>
<a id="__codelineno-5-30" name="__codelineno-5-30"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-5-31" name="__codelineno-5-31"></a>
<a id="__codelineno-5-32" name="__codelineno-5-32"></a><span class="w">    </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doPost</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-5-33" name="__codelineno-5-33"></a><span class="w">        </span><span class="n">doGet</span><span class="p">(</span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">response</span><span class="p">);</span>
<a id="__codelineno-5-34" name="__codelineno-5-34"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-5-35" name="__codelineno-5-35"></a>
<a id="__codelineno-5-36" name="__codelineno-5-36"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>重新部署后在浏览器中访问<code>http://localhost:8080/HelloServlet/getSessionInfo</code></p>
<!--=
    ??? tip "运行结果"
        <iframe src="./src/HelloServletResult/getSessionInfo.html" frameborder="0" width="100%" height="200"></iframe>
-->
</li>
</ol>
<h3 id="_4">综合</h3>
<p>使用Servlet实现基于四层架构（域模型层、数据访问层、服务层、控制层）的学生表的增、删、改、查操作，数据库使用Sqlite，Web API如下：</p>
<table>
<thead>
<tr>
<th>url</th>
<th>功能</th>
</tr>
</thead>
<tbody>
<tr>
<td>/demo/Student/Add</td>
<td>添加学生</td>
</tr>
<tr>
<td>/demo/Student/Delete</td>
<td>删除学生</td>
</tr>
<tr>
<td>/demo/Student/Update</td>
<td>更新学生</td>
</tr>
<tr>
<td>/demo/Student/Get</td>
<td>查询学生</td>
</tr>
</tbody>
</table>
<p>参考步骤如下：</p>
<ol>
<li>
<p>创建Java Web工程<strong>DaoTest</strong></p>
</li>
<li>
<p>将<code>sqlite-jdbc-3.41.0.0.jar</code>放入<strong>WEB-INF/lib</strong></p>
<blockquote>
<p>详见实验目录</p>
</blockquote>
</li>
<li>
<p>创建域模型<code>Student.java</code>，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/src/main/java/xust/stu/demo/domain/Student.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-6-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-6-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-6-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-6-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-6-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-6-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-6-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-6-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-6-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-6-10">10</a></span>
<span class="normal"><a href="#__codelineno-6-11">11</a></span>
<span class="normal"><a href="#__codelineno-6-12">12</a></span>
<span class="normal"><a href="#__codelineno-6-13">13</a></span>
<span class="normal"><a href="#__codelineno-6-14">14</a></span>
<span class="normal"><a href="#__codelineno-6-15">15</a></span>
<span class="normal"><a href="#__codelineno-6-16">16</a></span>
<span class="normal"><a href="#__codelineno-6-17">17</a></span>
<span class="normal"><a href="#__codelineno-6-18">18</a></span>
<span class="normal"><a href="#__codelineno-6-19">19</a></span>
<span class="normal"><a href="#__codelineno-6-20">20</a></span>
<span class="normal"><a href="#__codelineno-6-21">21</a></span>
<span class="normal"><a href="#__codelineno-6-22">22</a></span>
<span class="normal"><a href="#__codelineno-6-23">23</a></span>
<span class="normal"><a href="#__codelineno-6-24">24</a></span>
<span class="normal"><a href="#__codelineno-6-25">25</a></span>
<span class="normal"><a href="#__codelineno-6-26">26</a></span>
<span class="normal"><a href="#__codelineno-6-27">27</a></span>
<span class="normal"><a href="#__codelineno-6-28">28</a></span>
<span class="normal"><a href="#__codelineno-6-29">29</a></span>
<span class="normal"><a href="#__codelineno-6-30">30</a></span>
<span class="normal"><a href="#__codelineno-6-31">31</a></span>
<span class="normal"><a href="#__codelineno-6-32">32</a></span>
<span class="normal"><a href="#__codelineno-6-33">33</a></span>
<span class="normal"><a href="#__codelineno-6-34">34</a></span>
<span class="normal"><a href="#__codelineno-6-35">35</a></span>
<span class="normal"><a href="#__codelineno-6-36">36</a></span>
<span class="normal"><a href="#__codelineno-6-37">37</a></span>
<span class="normal"><a href="#__codelineno-6-38">38</a></span>
<span class="normal"><a href="#__codelineno-6-39">39</a></span>
<span class="normal"><a href="#__codelineno-6-40">40</a></span>
<span class="normal"><a href="#__codelineno-6-41">41</a></span>
<span class="normal"><a href="#__codelineno-6-42">42</a></span>
<span class="normal"><a href="#__codelineno-6-43">43</a></span>
<span class="normal"><a href="#__codelineno-6-44">44</a></span>
<span class="normal"><a href="#__codelineno-6-45">45</a></span>
<span class="normal"><a href="#__codelineno-6-46">46</a></span>
<span class="normal"><a href="#__codelineno-6-47">47</a></span>
<span class="normal"><a href="#__codelineno-6-48">48</a></span>
<span class="normal"><a href="#__codelineno-6-49">49</a></span>
<span class="normal"><a href="#__codelineno-6-50">50</a></span>
<span class="normal"><a href="#__codelineno-6-51">51</a></span>
<span class="normal"><a href="#__codelineno-6-52">52</a></span>
<span class="normal"><a href="#__codelineno-6-53">53</a></span>
<span class="normal"><a href="#__codelineno-6-54">54</a></span>
<span class="normal"><a href="#__codelineno-6-55">55</a></span>
<span class="normal"><a href="#__codelineno-6-56">56</a></span>
<span class="normal"><a href="#__codelineno-6-57">57</a></span>
<span class="normal"><a href="#__codelineno-6-58">58</a></span>
<span class="normal"><a href="#__codelineno-6-59">59</a></span>
<span class="normal"><a href="#__codelineno-6-60">60</a></span>
<span class="normal"><a href="#__codelineno-6-61">61</a></span>
<span class="normal"><a href="#__codelineno-6-62">62</a></span>
<span class="normal"><a href="#__codelineno-6-63">63</a></span>
<span class="normal"><a href="#__codelineno-6-64">64</a></span>
<span class="normal"><a href="#__codelineno-6-65">65</a></span>
<span class="normal"><a href="#__codelineno-6-66">66</a></span>
<span class="normal"><a href="#__codelineno-6-67">67</a></span>
<span class="normal"><a href="#__codelineno-6-68">68</a></span>
<span class="normal"><a href="#__codelineno-6-69">69</a></span>
<span class="normal"><a href="#__codelineno-6-70">70</a></span>
<span class="normal"><a href="#__codelineno-6-71">71</a></span>
<span class="normal"><a href="#__codelineno-6-72">72</a></span>
<span class="normal"><a href="#__codelineno-6-73">73</a></span>
<span class="normal"><a href="#__codelineno-6-74">74</a></span>
<span class="normal"><a href="#__codelineno-6-75">75</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu.demo.domain</span><span class="p">;</span>
<a id="__codelineno-6-2" name="__codelineno-6-2"></a>
<a id="__codelineno-6-3" name="__codelineno-6-3"></a><span class="cm">/**</span>
<a id="__codelineno-6-4" name="__codelineno-6-4"></a><span class="cm"> * Class Student</span>
<a id="__codelineno-6-5" name="__codelineno-6-5"></a><span class="cm"> * 学生表</span>
<a id="__codelineno-6-6" name="__codelineno-6-6"></a><span class="cm"> * @author XUST</span>
<a id="__codelineno-6-7" name="__codelineno-6-7"></a><span class="cm"> * @version 1.0, 2023-04-20</span>
<a id="__codelineno-6-8" name="__codelineno-6-8"></a><span class="cm"> */</span>
<a id="__codelineno-6-9" name="__codelineno-6-9"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Student</span><span class="p">{</span>
<a id="__codelineno-6-10" name="__codelineno-6-10"></a>
<a id="__codelineno-6-11" name="__codelineno-6-11"></a>
<a id="__codelineno-6-12" name="__codelineno-6-12"></a><span class="w">    </span><span class="cm">/**</span>
<a id="__codelineno-6-13" name="__codelineno-6-13"></a><span class="cm">     * 学号</span>
<a id="__codelineno-6-14" name="__codelineno-6-14"></a><span class="cm">     */</span>
<a id="__codelineno-6-15" name="__codelineno-6-15"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">;</span>
<a id="__codelineno-6-16" name="__codelineno-6-16"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getNo</span><span class="p">(){</span>
<a id="__codelineno-6-17" name="__codelineno-6-17"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">no</span><span class="p">;</span>
<a id="__codelineno-6-18" name="__codelineno-6-18"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-19" name="__codelineno-6-19"></a>
<a id="__codelineno-6-20" name="__codelineno-6-20"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">setNo</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">newValue</span><span class="p">){</span>
<a id="__codelineno-6-21" name="__codelineno-6-21"></a><span class="w">        </span><span class="n">no</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newValue</span><span class="p">;</span>
<a id="__codelineno-6-22" name="__codelineno-6-22"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-23" name="__codelineno-6-23"></a><span class="w">    </span><span class="cm">/**</span>
<a id="__codelineno-6-24" name="__codelineno-6-24"></a><span class="cm">     * 姓名</span>
<a id="__codelineno-6-25" name="__codelineno-6-25"></a><span class="cm">     */</span>
<a id="__codelineno-6-26" name="__codelineno-6-26"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<a id="__codelineno-6-27" name="__codelineno-6-27"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getName</span><span class="p">(){</span>
<a id="__codelineno-6-28" name="__codelineno-6-28"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">name</span><span class="p">;</span>
<a id="__codelineno-6-29" name="__codelineno-6-29"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-30" name="__codelineno-6-30"></a>
<a id="__codelineno-6-31" name="__codelineno-6-31"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">setName</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">newValue</span><span class="p">){</span>
<a id="__codelineno-6-32" name="__codelineno-6-32"></a><span class="w">        </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newValue</span><span class="p">;</span>
<a id="__codelineno-6-33" name="__codelineno-6-33"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-34" name="__codelineno-6-34"></a><span class="w">    </span><span class="cm">/**</span>
<a id="__codelineno-6-35" name="__codelineno-6-35"></a><span class="cm">     * 性别</span>
<a id="__codelineno-6-36" name="__codelineno-6-36"></a><span class="cm">     */</span>
<a id="__codelineno-6-37" name="__codelineno-6-37"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">gender</span><span class="p">;</span>
<a id="__codelineno-6-38" name="__codelineno-6-38"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getGender</span><span class="p">(){</span>
<a id="__codelineno-6-39" name="__codelineno-6-39"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">gender</span><span class="p">;</span>
<a id="__codelineno-6-40" name="__codelineno-6-40"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-41" name="__codelineno-6-41"></a>
<a id="__codelineno-6-42" name="__codelineno-6-42"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">setGender</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">newValue</span><span class="p">){</span>
<a id="__codelineno-6-43" name="__codelineno-6-43"></a><span class="w">        </span><span class="n">gender</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newValue</span><span class="p">;</span>
<a id="__codelineno-6-44" name="__codelineno-6-44"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-45" name="__codelineno-6-45"></a><span class="w">    </span><span class="cm">/**</span>
<a id="__codelineno-6-46" name="__codelineno-6-46"></a><span class="cm">     * 年龄</span>
<a id="__codelineno-6-47" name="__codelineno-6-47"></a><span class="cm">     */</span>
<a id="__codelineno-6-48" name="__codelineno-6-48"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="n">age</span><span class="p">;</span>
<a id="__codelineno-6-49" name="__codelineno-6-49"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="n">Integer</span><span class="w"> </span><span class="nf">getAge</span><span class="p">(){</span>
<a id="__codelineno-6-50" name="__codelineno-6-50"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">age</span><span class="p">;</span>
<a id="__codelineno-6-51" name="__codelineno-6-51"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-52" name="__codelineno-6-52"></a>
<a id="__codelineno-6-53" name="__codelineno-6-53"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">setAge</span><span class="p">(</span><span class="n">Integer</span><span class="w"> </span><span class="n">newValue</span><span class="p">){</span>
<a id="__codelineno-6-54" name="__codelineno-6-54"></a><span class="w">        </span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newValue</span><span class="p">;</span>
<a id="__codelineno-6-55" name="__codelineno-6-55"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-56" name="__codelineno-6-56"></a><span class="w">    </span><span class="cm">/**</span>
<a id="__codelineno-6-57" name="__codelineno-6-57"></a><span class="cm">     * 所在系</span>
<a id="__codelineno-6-58" name="__codelineno-6-58"></a><span class="cm">     */</span>
<a id="__codelineno-6-59" name="__codelineno-6-59"></a><span class="w">    </span><span class="kd">private</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">dept</span><span class="p">;</span>
<a id="__codelineno-6-60" name="__codelineno-6-60"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">getDept</span><span class="p">(){</span>
<a id="__codelineno-6-61" name="__codelineno-6-61"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">dept</span><span class="p">;</span>
<a id="__codelineno-6-62" name="__codelineno-6-62"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-63" name="__codelineno-6-63"></a>
<a id="__codelineno-6-64" name="__codelineno-6-64"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">setDept</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">newValue</span><span class="p">){</span>
<a id="__codelineno-6-65" name="__codelineno-6-65"></a><span class="w">        </span><span class="n">dept</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">newValue</span><span class="p">;</span>
<a id="__codelineno-6-66" name="__codelineno-6-66"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-67" name="__codelineno-6-67"></a>
<a id="__codelineno-6-68" name="__codelineno-6-68"></a><span class="w">    </span><span class="nd">@Override</span>
<a id="__codelineno-6-69" name="__codelineno-6-69"></a><span class="w">    </span><span class="cm">/**</span>
<a id="__codelineno-6-70" name="__codelineno-6-70"></a><span class="cm">     * 将对象序列化为JSON字符串</span>
<a id="__codelineno-6-71" name="__codelineno-6-71"></a><span class="cm">     */</span>
<a id="__codelineno-6-72" name="__codelineno-6-72"></a><span class="w">    </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="nf">toString</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-73" name="__codelineno-6-73"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="s">&quot;{&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;no\&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">no</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;, &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;name\&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;, &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;gender\&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">gender</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;, &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;age\&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;, &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;dept\&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">dept</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;\&quot;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w">  </span><span class="s">&quot;}&quot;</span><span class="p">;</span>
<a id="__codelineno-6-74" name="__codelineno-6-74"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-75" name="__codelineno-6-75"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>创建数据库连接类</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/xust/stu/ConnectionUtil.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-7-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-7-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-7-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-7-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-7-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-7-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-7-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-7-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-7-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-7-10">10</a></span>
<span class="normal"><a href="#__codelineno-7-11">11</a></span>
<span class="normal"><a href="#__codelineno-7-12">12</a></span>
<span class="normal"><a href="#__codelineno-7-13">13</a></span>
<span class="normal"><a href="#__codelineno-7-14">14</a></span>
<span class="normal"><a href="#__codelineno-7-15">15</a></span>
<span class="normal"><a href="#__codelineno-7-16">16</a></span>
<span class="normal"><a href="#__codelineno-7-17">17</a></span>
<span class="normal"><a href="#__codelineno-7-18">18</a></span>
<span class="normal"><a href="#__codelineno-7-19">19</a></span>
<span class="normal"><a href="#__codelineno-7-20">20</a></span>
<span class="normal"><a href="#__codelineno-7-21">21</a></span>
<span class="normal"><a href="#__codelineno-7-22">22</a></span>
<span class="normal"><a href="#__codelineno-7-23">23</a></span>
<span class="normal"><a href="#__codelineno-7-24">24</a></span>
<span class="normal"><a href="#__codelineno-7-25">25</a></span>
<span class="normal"><a href="#__codelineno-7-26">26</a></span>
<span class="normal"><a href="#__codelineno-7-27">27</a></span>
<span class="normal"><a href="#__codelineno-7-28">28</a></span>
<span class="normal"><a href="#__codelineno-7-29">29</a></span>
<span class="normal"><a href="#__codelineno-7-30">30</a></span>
<span class="normal"><a href="#__codelineno-7-31">31</a></span>
<span class="normal"><a href="#__codelineno-7-32">32</a></span>
<span class="normal"><a href="#__codelineno-7-33">33</a></span>
<span class="normal"><a href="#__codelineno-7-34">34</a></span>
<span class="normal"><a href="#__codelineno-7-35">35</a></span>
<span class="normal"><a href="#__codelineno-7-36">36</a></span>
<span class="normal"><a href="#__codelineno-7-37">37</a></span>
<span class="normal"><a href="#__codelineno-7-38">38</a></span>
<span class="normal"><a href="#__codelineno-7-39">39</a></span>
<span class="normal"><a href="#__codelineno-7-40">40</a></span>
<span class="normal"><a href="#__codelineno-7-41">41</a></span>
<span class="normal"><a href="#__codelineno-7-42">42</a></span>
<span class="normal"><a href="#__codelineno-7-43">43</a></span>
<span class="normal"><a href="#__codelineno-7-44">44</a></span>
<span class="normal"><a href="#__codelineno-7-45">45</a></span>
<span class="normal"><a href="#__codelineno-7-46">46</a></span>
<span class="normal"><a href="#__codelineno-7-47">47</a></span>
<span class="normal"><a href="#__codelineno-7-48">48</a></span>
<span class="normal"><a href="#__codelineno-7-49">49</a></span>
<span class="normal"><a href="#__codelineno-7-50">50</a></span>
<span class="normal"><a href="#__codelineno-7-51">51</a></span>
<span class="normal"><a href="#__codelineno-7-52">52</a></span>
<span class="normal"><a href="#__codelineno-7-53">53</a></span>
<span class="normal"><a href="#__codelineno-7-54">54</a></span>
<span class="normal"><a href="#__codelineno-7-55">55</a></span>
<span class="normal"><a href="#__codelineno-7-56">56</a></span>
<span class="normal"><a href="#__codelineno-7-57">57</a></span>
<span class="normal"><a href="#__codelineno-7-58">58</a></span>
<span class="normal"><a href="#__codelineno-7-59">59</a></span>
<span class="normal"><a href="#__codelineno-7-60">60</a></span>
<span class="normal"><a href="#__codelineno-7-61">61</a></span>
<span class="normal"><a href="#__codelineno-7-62">62</a></span>
<span class="normal"><a href="#__codelineno-7-63">63</a></span>
<span class="normal"><a href="#__codelineno-7-64">64</a></span>
<span class="normal"><a href="#__codelineno-7-65">65</a></span>
<span class="normal"><a href="#__codelineno-7-66">66</a></span>
<span class="normal"><a href="#__codelineno-7-67">67</a></span>
<span class="normal"><a href="#__codelineno-7-68">68</a></span>
<span class="normal"><a href="#__codelineno-7-69">69</a></span>
<span class="normal"><a href="#__codelineno-7-70">70</a></span>
<span class="normal"><a href="#__codelineno-7-71">71</a></span>
<span class="normal"><a href="#__codelineno-7-72">72</a></span>
<span class="normal"><a href="#__codelineno-7-73">73</a></span>
<span class="normal"><a href="#__codelineno-7-74">74</a></span>
<span class="normal"><a href="#__codelineno-7-75">75</a></span>
<span class="normal"><a href="#__codelineno-7-76">76</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu</span><span class="p">;</span>
<a id="__codelineno-7-2" name="__codelineno-7-2"></a>
<a id="__codelineno-7-3" name="__codelineno-7-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.sql.*</span><span class="p">;</span>
<a id="__codelineno-7-4" name="__codelineno-7-4"></a>
<a id="__codelineno-7-5" name="__codelineno-7-5"></a><span class="cm">/**</span>
<a id="__codelineno-7-6" name="__codelineno-7-6"></a><span class="cm"> * 连接辅助类</span>
<a id="__codelineno-7-7" name="__codelineno-7-7"></a><span class="cm"> */</span>
<a id="__codelineno-7-8" name="__codelineno-7-8"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">ConnectionUtil</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-9" name="__codelineno-7-9"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-7-10" name="__codelineno-7-10"></a><span class="cm">   * 创建连接</span>
<a id="__codelineno-7-11" name="__codelineno-7-11"></a><span class="cm">   * </span>
<a id="__codelineno-7-12" name="__codelineno-7-12"></a><span class="cm">   * @return 连接对象</span>
<a id="__codelineno-7-13" name="__codelineno-7-13"></a><span class="cm">   */</span>
<a id="__codelineno-7-14" name="__codelineno-7-14"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="n">Connection</span><span class="w"> </span><span class="nf">getConnection</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-15" name="__codelineno-7-15"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-7-16" name="__codelineno-7-16"></a>
<a id="__codelineno-7-17" name="__codelineno-7-17"></a><span class="w">    </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-18" name="__codelineno-7-18"></a><span class="w">      </span><span class="n">Class</span><span class="p">.</span><span class="na">forName</span><span class="p">(</span><span class="s">&quot;org.sqlite.JDBC&quot;</span><span class="p">);</span>
<a id="__codelineno-7-19" name="__codelineno-7-19"></a><span class="w">      </span><span class="c1">//在数据库文件stu.db存入在D盘下</span>
<a id="__codelineno-7-20" name="__codelineno-7-20"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">DriverManager</span><span class="p">.</span><span class="na">getConnection</span><span class="p">(</span><span class="s">&quot;jdbc:sqlite:d:/stu.db&quot;</span><span class="p">);</span>
<a id="__codelineno-7-21" name="__codelineno-7-21"></a><span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-22" name="__codelineno-7-22"></a><span class="w">      </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-7-23" name="__codelineno-7-23"></a><span class="w">      </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-7-24" name="__codelineno-7-24"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-7-25" name="__codelineno-7-25"></a>
<a id="__codelineno-7-26" name="__codelineno-7-26"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-7-27" name="__codelineno-7-27"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-7-28" name="__codelineno-7-28"></a>
<a id="__codelineno-7-29" name="__codelineno-7-29"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-7-30" name="__codelineno-7-30"></a><span class="cm">   * 关闭连接</span>
<a id="__codelineno-7-31" name="__codelineno-7-31"></a><span class="cm">   * </span>
<a id="__codelineno-7-32" name="__codelineno-7-32"></a><span class="cm">   * @param connection 连接对象</span>
<a id="__codelineno-7-33" name="__codelineno-7-33"></a><span class="cm">   */</span>
<a id="__codelineno-7-34" name="__codelineno-7-34"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">closeConnection</span><span class="p">(</span><span class="n">Connection</span><span class="w"> </span><span class="n">connection</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-35" name="__codelineno-7-35"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">connection</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-36" name="__codelineno-7-36"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-37" name="__codelineno-7-37"></a><span class="w">        </span><span class="n">connection</span><span class="p">.</span><span class="na">close</span><span class="p">();</span>
<a id="__codelineno-7-38" name="__codelineno-7-38"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-39" name="__codelineno-7-39"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-7-40" name="__codelineno-7-40"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-7-41" name="__codelineno-7-41"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-7-42" name="__codelineno-7-42"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-7-43" name="__codelineno-7-43"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-7-44" name="__codelineno-7-44"></a>
<a id="__codelineno-7-45" name="__codelineno-7-45"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-7-46" name="__codelineno-7-46"></a><span class="cm">   * 关闭语句</span>
<a id="__codelineno-7-47" name="__codelineno-7-47"></a><span class="cm">   * </span>
<a id="__codelineno-7-48" name="__codelineno-7-48"></a><span class="cm">   * @param pstmt 语句对象</span>
<a id="__codelineno-7-49" name="__codelineno-7-49"></a><span class="cm">   */</span>
<a id="__codelineno-7-50" name="__codelineno-7-50"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">closePstmt</span><span class="p">(</span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">pstmt</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-51" name="__codelineno-7-51"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pstmt</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-52" name="__codelineno-7-52"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-53" name="__codelineno-7-53"></a><span class="w">        </span><span class="n">pstmt</span><span class="p">.</span><span class="na">close</span><span class="p">();</span>
<a id="__codelineno-7-54" name="__codelineno-7-54"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-55" name="__codelineno-7-55"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-7-56" name="__codelineno-7-56"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-7-57" name="__codelineno-7-57"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-7-58" name="__codelineno-7-58"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-7-59" name="__codelineno-7-59"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-7-60" name="__codelineno-7-60"></a>
<a id="__codelineno-7-61" name="__codelineno-7-61"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-7-62" name="__codelineno-7-62"></a><span class="cm">   * 关闭语句</span>
<a id="__codelineno-7-63" name="__codelineno-7-63"></a><span class="cm">   * </span>
<a id="__codelineno-7-64" name="__codelineno-7-64"></a><span class="cm">   * @param pstmt 语句对象</span>
<a id="__codelineno-7-65" name="__codelineno-7-65"></a><span class="cm">   */</span>
<a id="__codelineno-7-66" name="__codelineno-7-66"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">closeStmt</span><span class="p">(</span><span class="n">Statement</span><span class="w"> </span><span class="n">stmt</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-67" name="__codelineno-7-67"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">stmt</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-68" name="__codelineno-7-68"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-69" name="__codelineno-7-69"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">close</span><span class="p">();</span>
<a id="__codelineno-7-70" name="__codelineno-7-70"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-71" name="__codelineno-7-71"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-7-72" name="__codelineno-7-72"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-7-73" name="__codelineno-7-73"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-7-74" name="__codelineno-7-74"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-7-75" name="__codelineno-7-75"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-7-76" name="__codelineno-7-76"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
<!--
    ??? tip "源代码"
        <div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/xust/stu/ConnectionUtil.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-8-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-8-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-8-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-8-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-8-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-8-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-8-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-8-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-8-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-8-10">10</a></span>
<span class="normal"><a href="#__codelineno-8-11">11</a></span>
<span class="normal"><a href="#__codelineno-8-12">12</a></span>
<span class="normal"><a href="#__codelineno-8-13">13</a></span>
<span class="normal"><a href="#__codelineno-8-14">14</a></span>
<span class="normal"><a href="#__codelineno-8-15">15</a></span>
<span class="normal"><a href="#__codelineno-8-16">16</a></span>
<span class="normal"><a href="#__codelineno-8-17">17</a></span>
<span class="normal"><a href="#__codelineno-8-18">18</a></span>
<span class="normal"><a href="#__codelineno-8-19">19</a></span>
<span class="normal"><a href="#__codelineno-8-20">20</a></span>
<span class="normal"><a href="#__codelineno-8-21">21</a></span>
<span class="normal"><a href="#__codelineno-8-22">22</a></span>
<span class="normal"><a href="#__codelineno-8-23">23</a></span>
<span class="normal"><a href="#__codelineno-8-24">24</a></span>
<span class="normal"><a href="#__codelineno-8-25">25</a></span>
<span class="normal"><a href="#__codelineno-8-26">26</a></span>
<span class="normal"><a href="#__codelineno-8-27">27</a></span>
<span class="normal"><a href="#__codelineno-8-28">28</a></span>
<span class="normal"><a href="#__codelineno-8-29">29</a></span>
<span class="normal"><a href="#__codelineno-8-30">30</a></span>
<span class="normal"><a href="#__codelineno-8-31">31</a></span>
<span class="normal"><a href="#__codelineno-8-32">32</a></span>
<span class="normal"><a href="#__codelineno-8-33">33</a></span>
<span class="normal"><a href="#__codelineno-8-34">34</a></span>
<span class="normal"><a href="#__codelineno-8-35">35</a></span>
<span class="normal"><a href="#__codelineno-8-36">36</a></span>
<span class="normal"><a href="#__codelineno-8-37">37</a></span>
<span class="normal"><a href="#__codelineno-8-38">38</a></span>
<span class="normal"><a href="#__codelineno-8-39">39</a></span>
<span class="normal"><a href="#__codelineno-8-40">40</a></span>
<span class="normal"><a href="#__codelineno-8-41">41</a></span>
<span class="normal"><a href="#__codelineno-8-42">42</a></span>
<span class="normal"><a href="#__codelineno-8-43">43</a></span>
<span class="normal"><a href="#__codelineno-8-44">44</a></span>
<span class="normal"><a href="#__codelineno-8-45">45</a></span>
<span class="normal"><a href="#__codelineno-8-46">46</a></span>
<span class="normal"><a href="#__codelineno-8-47">47</a></span>
<span class="normal"><a href="#__codelineno-8-48">48</a></span>
<span class="normal"><a href="#__codelineno-8-49">49</a></span>
<span class="normal"><a href="#__codelineno-8-50">50</a></span>
<span class="normal"><a href="#__codelineno-8-51">51</a></span>
<span class="normal"><a href="#__codelineno-8-52">52</a></span>
<span class="normal"><a href="#__codelineno-8-53">53</a></span>
<span class="normal"><a href="#__codelineno-8-54">54</a></span>
<span class="normal"><a href="#__codelineno-8-55">55</a></span>
<span class="normal"><a href="#__codelineno-8-56">56</a></span>
<span class="normal"><a href="#__codelineno-8-57">57</a></span>
<span class="normal"><a href="#__codelineno-8-58">58</a></span>
<span class="normal"><a href="#__codelineno-8-59">59</a></span>
<span class="normal"><a href="#__codelineno-8-60">60</a></span>
<span class="normal"><a href="#__codelineno-8-61">61</a></span>
<span class="normal"><a href="#__codelineno-8-62">62</a></span>
<span class="normal"><a href="#__codelineno-8-63">63</a></span>
<span class="normal"><a href="#__codelineno-8-64">64</a></span>
<span class="normal"><a href="#__codelineno-8-65">65</a></span>
<span class="normal"><a href="#__codelineno-8-66">66</a></span>
<span class="normal"><a href="#__codelineno-8-67">67</a></span>
<span class="normal"><a href="#__codelineno-8-68">68</a></span>
<span class="normal"><a href="#__codelineno-8-69">69</a></span>
<span class="normal"><a href="#__codelineno-8-70">70</a></span>
<span class="normal"><a href="#__codelineno-8-71">71</a></span>
<span class="normal"><a href="#__codelineno-8-72">72</a></span>
<span class="normal"><a href="#__codelineno-8-73">73</a></span>
<span class="normal"><a href="#__codelineno-8-74">74</a></span>
<span class="normal"><a href="#__codelineno-8-75">75</a></span>
<span class="normal"><a href="#__codelineno-8-76">76</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu</span><span class="p">;</span>
<a id="__codelineno-8-2" name="__codelineno-8-2"></a>
<a id="__codelineno-8-3" name="__codelineno-8-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.sql.*</span><span class="p">;</span>
<a id="__codelineno-8-4" name="__codelineno-8-4"></a>
<a id="__codelineno-8-5" name="__codelineno-8-5"></a><span class="cm">/**</span>
<a id="__codelineno-8-6" name="__codelineno-8-6"></a><span class="cm"> * 连接辅助类</span>
<a id="__codelineno-8-7" name="__codelineno-8-7"></a><span class="cm"> */</span>
<a id="__codelineno-8-8" name="__codelineno-8-8"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">ConnectionUtil</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-9" name="__codelineno-8-9"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-8-10" name="__codelineno-8-10"></a><span class="cm">   * 创建连接</span>
<a id="__codelineno-8-11" name="__codelineno-8-11"></a><span class="cm">   * </span>
<a id="__codelineno-8-12" name="__codelineno-8-12"></a><span class="cm">   * @return 连接对象</span>
<a id="__codelineno-8-13" name="__codelineno-8-13"></a><span class="cm">   */</span>
<a id="__codelineno-8-14" name="__codelineno-8-14"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="n">Connection</span><span class="w"> </span><span class="nf">getConnection</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-15" name="__codelineno-8-15"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-8-16" name="__codelineno-8-16"></a>
<a id="__codelineno-8-17" name="__codelineno-8-17"></a><span class="w">    </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-18" name="__codelineno-8-18"></a><span class="w">      </span><span class="n">Class</span><span class="p">.</span><span class="na">forName</span><span class="p">(</span><span class="s">&quot;org.sqlite.JDBC&quot;</span><span class="p">);</span>
<a id="__codelineno-8-19" name="__codelineno-8-19"></a><span class="w">      </span><span class="c1">//在数据库文件stu.db存入在D盘下</span>
<a id="__codelineno-8-20" name="__codelineno-8-20"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">DriverManager</span><span class="p">.</span><span class="na">getConnection</span><span class="p">(</span><span class="s">&quot;jdbc:sqlite:d:/stu.db&quot;</span><span class="p">);</span>
<a id="__codelineno-8-21" name="__codelineno-8-21"></a><span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-22" name="__codelineno-8-22"></a><span class="w">      </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-8-23" name="__codelineno-8-23"></a><span class="w">      </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-8-24" name="__codelineno-8-24"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-8-25" name="__codelineno-8-25"></a>
<a id="__codelineno-8-26" name="__codelineno-8-26"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-8-27" name="__codelineno-8-27"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-8-28" name="__codelineno-8-28"></a>
<a id="__codelineno-8-29" name="__codelineno-8-29"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-8-30" name="__codelineno-8-30"></a><span class="cm">   * 关闭连接</span>
<a id="__codelineno-8-31" name="__codelineno-8-31"></a><span class="cm">   * </span>
<a id="__codelineno-8-32" name="__codelineno-8-32"></a><span class="cm">   * @param connection 连接对象</span>
<a id="__codelineno-8-33" name="__codelineno-8-33"></a><span class="cm">   */</span>
<a id="__codelineno-8-34" name="__codelineno-8-34"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">closeConnection</span><span class="p">(</span><span class="n">Connection</span><span class="w"> </span><span class="n">connection</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-35" name="__codelineno-8-35"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">connection</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-36" name="__codelineno-8-36"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-37" name="__codelineno-8-37"></a><span class="w">        </span><span class="n">connection</span><span class="p">.</span><span class="na">close</span><span class="p">();</span>
<a id="__codelineno-8-38" name="__codelineno-8-38"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-39" name="__codelineno-8-39"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-8-40" name="__codelineno-8-40"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-8-41" name="__codelineno-8-41"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-8-42" name="__codelineno-8-42"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-8-43" name="__codelineno-8-43"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-8-44" name="__codelineno-8-44"></a>
<a id="__codelineno-8-45" name="__codelineno-8-45"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-8-46" name="__codelineno-8-46"></a><span class="cm">   * 关闭语句</span>
<a id="__codelineno-8-47" name="__codelineno-8-47"></a><span class="cm">   * </span>
<a id="__codelineno-8-48" name="__codelineno-8-48"></a><span class="cm">   * @param pstmt 语句对象</span>
<a id="__codelineno-8-49" name="__codelineno-8-49"></a><span class="cm">   */</span>
<a id="__codelineno-8-50" name="__codelineno-8-50"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">closePstmt</span><span class="p">(</span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">pstmt</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-51" name="__codelineno-8-51"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pstmt</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-52" name="__codelineno-8-52"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-53" name="__codelineno-8-53"></a><span class="w">        </span><span class="n">pstmt</span><span class="p">.</span><span class="na">close</span><span class="p">();</span>
<a id="__codelineno-8-54" name="__codelineno-8-54"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-55" name="__codelineno-8-55"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-8-56" name="__codelineno-8-56"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-8-57" name="__codelineno-8-57"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-8-58" name="__codelineno-8-58"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-8-59" name="__codelineno-8-59"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-8-60" name="__codelineno-8-60"></a>
<a id="__codelineno-8-61" name="__codelineno-8-61"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-8-62" name="__codelineno-8-62"></a><span class="cm">   * 关闭语句</span>
<a id="__codelineno-8-63" name="__codelineno-8-63"></a><span class="cm">   * </span>
<a id="__codelineno-8-64" name="__codelineno-8-64"></a><span class="cm">   * @param pstmt 语句对象</span>
<a id="__codelineno-8-65" name="__codelineno-8-65"></a><span class="cm">   */</span>
<a id="__codelineno-8-66" name="__codelineno-8-66"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">closeStmt</span><span class="p">(</span><span class="n">Statement</span><span class="w"> </span><span class="n">stmt</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-67" name="__codelineno-8-67"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">stmt</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-68" name="__codelineno-8-68"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-69" name="__codelineno-8-69"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">close</span><span class="p">();</span>
<a id="__codelineno-8-70" name="__codelineno-8-70"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-71" name="__codelineno-8-71"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">err</span><span class="p">.</span><span class="na">println</span><span class="p">(</span><span class="n">e</span><span class="p">.</span><span class="na">getClass</span><span class="p">().</span><span class="na">getName</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">&quot;: &quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">());</span>
<a id="__codelineno-8-72" name="__codelineno-8-72"></a><span class="w">        </span><span class="n">System</span><span class="p">.</span><span class="na">exit</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<a id="__codelineno-8-73" name="__codelineno-8-73"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-8-74" name="__codelineno-8-74"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-8-75" name="__codelineno-8-75"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-8-76" name="__codelineno-8-76"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
-->
</li>
<li>
<p>创建结果对象<code>Result.java</code>，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/src/main/java/xust/stu/Result.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-9-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-9-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-9-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-9-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-9-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-9-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-9-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-9-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-9-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-9-10">10</a></span>
<span class="normal"><a href="#__codelineno-9-11">11</a></span>
<span class="normal"><a href="#__codelineno-9-12">12</a></span>
<span class="normal"><a href="#__codelineno-9-13">13</a></span>
<span class="normal"><a href="#__codelineno-9-14">14</a></span>
<span class="normal"><a href="#__codelineno-9-15">15</a></span>
<span class="normal"><a href="#__codelineno-9-16">16</a></span>
<span class="normal"><a href="#__codelineno-9-17">17</a></span>
<span class="normal"><a href="#__codelineno-9-18">18</a></span>
<span class="normal"><a href="#__codelineno-9-19">19</a></span>
<span class="normal"><a href="#__codelineno-9-20">20</a></span>
<span class="normal"><a href="#__codelineno-9-21">21</a></span>
<span class="normal"><a href="#__codelineno-9-22">22</a></span>
<span class="normal"><a href="#__codelineno-9-23">23</a></span>
<span class="normal"><a href="#__codelineno-9-24">24</a></span>
<span class="normal"><a href="#__codelineno-9-25">25</a></span>
<span class="normal"><a href="#__codelineno-9-26">26</a></span>
<span class="normal"><a href="#__codelineno-9-27">27</a></span>
<span class="normal"><a href="#__codelineno-9-28">28</a></span>
<span class="normal"><a href="#__codelineno-9-29">29</a></span>
<span class="normal"><a href="#__codelineno-9-30">30</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu</span><span class="p">;</span>
<a id="__codelineno-9-2" name="__codelineno-9-2"></a>
<a id="__codelineno-9-3" name="__codelineno-9-3"></a><span class="cm">/**</span>
<a id="__codelineno-9-4" name="__codelineno-9-4"></a><span class="cm"> * 操作结果类</span>
<a id="__codelineno-9-5" name="__codelineno-9-5"></a><span class="cm"> */</span>
<a id="__codelineno-9-6" name="__codelineno-9-6"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">Result</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-9-7" name="__codelineno-9-7"></a>
<a id="__codelineno-9-8" name="__codelineno-9-8"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-9-9" name="__codelineno-9-9"></a><span class="cm">   * 结果码，0为成功</span>
<a id="__codelineno-9-10" name="__codelineno-9-10"></a><span class="cm">   */</span>
<a id="__codelineno-9-11" name="__codelineno-9-11"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">code</span><span class="p">;</span>
<a id="__codelineno-9-12" name="__codelineno-9-12"></a>
<a id="__codelineno-9-13" name="__codelineno-9-13"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-9-14" name="__codelineno-9-14"></a><span class="cm">   * 结果信息</span>
<a id="__codelineno-9-15" name="__codelineno-9-15"></a><span class="cm">   */</span>
<a id="__codelineno-9-16" name="__codelineno-9-16"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">String</span><span class="w"> </span><span class="n">message</span><span class="p">;</span>
<a id="__codelineno-9-17" name="__codelineno-9-17"></a>
<a id="__codelineno-9-18" name="__codelineno-9-18"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-9-19" name="__codelineno-9-19"></a><span class="cm">   * 结果数据</span>
<a id="__codelineno-9-20" name="__codelineno-9-20"></a><span class="cm">   */</span>
<a id="__codelineno-9-21" name="__codelineno-9-21"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Object</span><span class="w"> </span><span class="n">data</span><span class="p">;</span>
<a id="__codelineno-9-22" name="__codelineno-9-22"></a>
<a id="__codelineno-9-23" name="__codelineno-9-23"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="nf">Result</span><span class="p">(){</span>
<a id="__codelineno-9-24" name="__codelineno-9-24"></a>
<a id="__codelineno-9-25" name="__codelineno-9-25"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-9-26" name="__codelineno-9-26"></a>
<a id="__codelineno-9-27" name="__codelineno-9-27"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="nf">Result</span><span class="p">(</span><span class="n">Boolean</span><span class="w"> </span><span class="n">v</span><span class="p">){</span>
<a id="__codelineno-9-28" name="__codelineno-9-28"></a><span class="w">    </span><span class="n">code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<a id="__codelineno-9-29" name="__codelineno-9-29"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-9-30" name="__codelineno-9-30"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>创建数据访问对象<code>StudentDao.java</code>和<code>StudentDaoImpl.java</code>，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/src/main/java/xust/stu/demo/dao/StudentDao.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-10-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-10-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-10-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-10-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-10-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-10-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-10-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-10-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-10-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-10-10">10</a></span>
<span class="normal"><a href="#__codelineno-10-11">11</a></span>
<span class="normal"><a href="#__codelineno-10-12">12</a></span>
<span class="normal"><a href="#__codelineno-10-13">13</a></span>
<span class="normal"><a href="#__codelineno-10-14">14</a></span>
<span class="normal"><a href="#__codelineno-10-15">15</a></span>
<span class="normal"><a href="#__codelineno-10-16">16</a></span>
<span class="normal"><a href="#__codelineno-10-17">17</a></span>
<span class="normal"><a href="#__codelineno-10-18">18</a></span>
<span class="normal"><a href="#__codelineno-10-19">19</a></span>
<span class="normal"><a href="#__codelineno-10-20">20</a></span>
<span class="normal"><a href="#__codelineno-10-21">21</a></span>
<span class="normal"><a href="#__codelineno-10-22">22</a></span>
<span class="normal"><a href="#__codelineno-10-23">23</a></span>
<span class="normal"><a href="#__codelineno-10-24">24</a></span>
<span class="normal"><a href="#__codelineno-10-25">25</a></span>
<span class="normal"><a href="#__codelineno-10-26">26</a></span>
<span class="normal"><a href="#__codelineno-10-27">27</a></span>
<span class="normal"><a href="#__codelineno-10-28">28</a></span>
<span class="normal"><a href="#__codelineno-10-29">29</a></span>
<span class="normal"><a href="#__codelineno-10-30">30</a></span>
<span class="normal"><a href="#__codelineno-10-31">31</a></span>
<span class="normal"><a href="#__codelineno-10-32">32</a></span>
<span class="normal"><a href="#__codelineno-10-33">33</a></span>
<span class="normal"><a href="#__codelineno-10-34">34</a></span>
<span class="normal"><a href="#__codelineno-10-35">35</a></span>
<span class="normal"><a href="#__codelineno-10-36">36</a></span>
<span class="normal"><a href="#__codelineno-10-37">37</a></span>
<span class="normal"><a href="#__codelineno-10-38">38</a></span>
<span class="normal"><a href="#__codelineno-10-39">39</a></span>
<span class="normal"><a href="#__codelineno-10-40">40</a></span>
<span class="normal"><a href="#__codelineno-10-41">41</a></span>
<span class="normal"><a href="#__codelineno-10-42">42</a></span>
<span class="normal"><a href="#__codelineno-10-43">43</a></span>
<span class="normal"><a href="#__codelineno-10-44">44</a></span>
<span class="normal"><a href="#__codelineno-10-45">45</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu.demo.dao</span><span class="p">;</span>
<a id="__codelineno-10-2" name="__codelineno-10-2"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.Result</span><span class="p">;</span>
<a id="__codelineno-10-3" name="__codelineno-10-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.domain.Student</span><span class="p">;</span>
<a id="__codelineno-10-4" name="__codelineno-10-4"></a>
<a id="__codelineno-10-5" name="__codelineno-10-5"></a><span class="kd">public</span><span class="w"> </span><span class="kd">interface</span> <span class="nc">StudentDao</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-10-6" name="__codelineno-10-6"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-10-7" name="__codelineno-10-7"></a><span class="cm">   * 初始化表</span>
<a id="__codelineno-10-8" name="__codelineno-10-8"></a><span class="cm">   * @return</span>
<a id="__codelineno-10-9" name="__codelineno-10-9"></a><span class="cm">   */</span>
<a id="__codelineno-10-10" name="__codelineno-10-10"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">init</span><span class="p">();</span>
<a id="__codelineno-10-11" name="__codelineno-10-11"></a>
<a id="__codelineno-10-12" name="__codelineno-10-12"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-10-13" name="__codelineno-10-13"></a><span class="cm">   * 增</span>
<a id="__codelineno-10-14" name="__codelineno-10-14"></a><span class="cm">   * @param o</span>
<a id="__codelineno-10-15" name="__codelineno-10-15"></a><span class="cm">   * @return</span>
<a id="__codelineno-10-16" name="__codelineno-10-16"></a><span class="cm">   */</span>
<a id="__codelineno-10-17" name="__codelineno-10-17"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">create</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">);</span>
<a id="__codelineno-10-18" name="__codelineno-10-18"></a>
<a id="__codelineno-10-19" name="__codelineno-10-19"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-10-20" name="__codelineno-10-20"></a><span class="cm">   * 删</span>
<a id="__codelineno-10-21" name="__codelineno-10-21"></a><span class="cm">   * @param id</span>
<a id="__codelineno-10-22" name="__codelineno-10-22"></a><span class="cm">   * @return</span>
<a id="__codelineno-10-23" name="__codelineno-10-23"></a><span class="cm">   */</span>
<a id="__codelineno-10-24" name="__codelineno-10-24"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">delete</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-10-25" name="__codelineno-10-25"></a>
<a id="__codelineno-10-26" name="__codelineno-10-26"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-10-27" name="__codelineno-10-27"></a><span class="cm">   * 改</span>
<a id="__codelineno-10-28" name="__codelineno-10-28"></a><span class="cm">   * @param o</span>
<a id="__codelineno-10-29" name="__codelineno-10-29"></a><span class="cm">   * @return</span>
<a id="__codelineno-10-30" name="__codelineno-10-30"></a><span class="cm">   */</span>
<a id="__codelineno-10-31" name="__codelineno-10-31"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">update</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">);</span>
<a id="__codelineno-10-32" name="__codelineno-10-32"></a>
<a id="__codelineno-10-33" name="__codelineno-10-33"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-10-34" name="__codelineno-10-34"></a><span class="cm">   * 查</span>
<a id="__codelineno-10-35" name="__codelineno-10-35"></a><span class="cm">   * @param no</span>
<a id="__codelineno-10-36" name="__codelineno-10-36"></a><span class="cm">   * @return</span>
<a id="__codelineno-10-37" name="__codelineno-10-37"></a><span class="cm">   */</span>
<a id="__codelineno-10-38" name="__codelineno-10-38"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">get</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-10-39" name="__codelineno-10-39"></a>
<a id="__codelineno-10-40" name="__codelineno-10-40"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-10-41" name="__codelineno-10-41"></a><span class="cm">   * 查</span>
<a id="__codelineno-10-42" name="__codelineno-10-42"></a><span class="cm">   * @return</span>
<a id="__codelineno-10-43" name="__codelineno-10-43"></a><span class="cm">   */</span>
<a id="__codelineno-10-44" name="__codelineno-10-44"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">getAll</span><span class="p">();</span>
<a id="__codelineno-10-45" name="__codelineno-10-45"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/src/main/java/xust/stu/demo/dao/StudentDaoImpl.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-11-1">  1</a></span>
<span class="normal"><a href="#__codelineno-11-2">  2</a></span>
<span class="normal"><a href="#__codelineno-11-3">  3</a></span>
<span class="normal"><a href="#__codelineno-11-4">  4</a></span>
<span class="normal"><a href="#__codelineno-11-5">  5</a></span>
<span class="normal"><a href="#__codelineno-11-6">  6</a></span>
<span class="normal"><a href="#__codelineno-11-7">  7</a></span>
<span class="normal"><a href="#__codelineno-11-8">  8</a></span>
<span class="normal"><a href="#__codelineno-11-9">  9</a></span>
<span class="normal"><a href="#__codelineno-11-10"> 10</a></span>
<span class="normal"><a href="#__codelineno-11-11"> 11</a></span>
<span class="normal"><a href="#__codelineno-11-12"> 12</a></span>
<span class="normal"><a href="#__codelineno-11-13"> 13</a></span>
<span class="normal"><a href="#__codelineno-11-14"> 14</a></span>
<span class="normal"><a href="#__codelineno-11-15"> 15</a></span>
<span class="normal"><a href="#__codelineno-11-16"> 16</a></span>
<span class="normal"><a href="#__codelineno-11-17"> 17</a></span>
<span class="normal"><a href="#__codelineno-11-18"> 18</a></span>
<span class="normal"><a href="#__codelineno-11-19"> 19</a></span>
<span class="normal"><a href="#__codelineno-11-20"> 20</a></span>
<span class="normal"><a href="#__codelineno-11-21"> 21</a></span>
<span class="normal"><a href="#__codelineno-11-22"> 22</a></span>
<span class="normal"><a href="#__codelineno-11-23"> 23</a></span>
<span class="normal"><a href="#__codelineno-11-24"> 24</a></span>
<span class="normal"><a href="#__codelineno-11-25"> 25</a></span>
<span class="normal"><a href="#__codelineno-11-26"> 26</a></span>
<span class="normal"><a href="#__codelineno-11-27"> 27</a></span>
<span class="normal"><a href="#__codelineno-11-28"> 28</a></span>
<span class="normal"><a href="#__codelineno-11-29"> 29</a></span>
<span class="normal"><a href="#__codelineno-11-30"> 30</a></span>
<span class="normal"><a href="#__codelineno-11-31"> 31</a></span>
<span class="normal"><a href="#__codelineno-11-32"> 32</a></span>
<span class="normal"><a href="#__codelineno-11-33"> 33</a></span>
<span class="normal"><a href="#__codelineno-11-34"> 34</a></span>
<span class="normal"><a href="#__codelineno-11-35"> 35</a></span>
<span class="normal"><a href="#__codelineno-11-36"> 36</a></span>
<span class="normal"><a href="#__codelineno-11-37"> 37</a></span>
<span class="normal"><a href="#__codelineno-11-38"> 38</a></span>
<span class="normal"><a href="#__codelineno-11-39"> 39</a></span>
<span class="normal"><a href="#__codelineno-11-40"> 40</a></span>
<span class="normal"><a href="#__codelineno-11-41"> 41</a></span>
<span class="normal"><a href="#__codelineno-11-42"> 42</a></span>
<span class="normal"><a href="#__codelineno-11-43"> 43</a></span>
<span class="normal"><a href="#__codelineno-11-44"> 44</a></span>
<span class="normal"><a href="#__codelineno-11-45"> 45</a></span>
<span class="normal"><a href="#__codelineno-11-46"> 46</a></span>
<span class="normal"><a href="#__codelineno-11-47"> 47</a></span>
<span class="normal"><a href="#__codelineno-11-48"> 48</a></span>
<span class="normal"><a href="#__codelineno-11-49"> 49</a></span>
<span class="normal"><a href="#__codelineno-11-50"> 50</a></span>
<span class="normal"><a href="#__codelineno-11-51"> 51</a></span>
<span class="normal"><a href="#__codelineno-11-52"> 52</a></span>
<span class="normal"><a href="#__codelineno-11-53"> 53</a></span>
<span class="normal"><a href="#__codelineno-11-54"> 54</a></span>
<span class="normal"><a href="#__codelineno-11-55"> 55</a></span>
<span class="normal"><a href="#__codelineno-11-56"> 56</a></span>
<span class="normal"><a href="#__codelineno-11-57"> 57</a></span>
<span class="normal"><a href="#__codelineno-11-58"> 58</a></span>
<span class="normal"><a href="#__codelineno-11-59"> 59</a></span>
<span class="normal"><a href="#__codelineno-11-60"> 60</a></span>
<span class="normal"><a href="#__codelineno-11-61"> 61</a></span>
<span class="normal"><a href="#__codelineno-11-62"> 62</a></span>
<span class="normal"><a href="#__codelineno-11-63"> 63</a></span>
<span class="normal"><a href="#__codelineno-11-64"> 64</a></span>
<span class="normal"><a href="#__codelineno-11-65"> 65</a></span>
<span class="normal"><a href="#__codelineno-11-66"> 66</a></span>
<span class="normal"><a href="#__codelineno-11-67"> 67</a></span>
<span class="normal"><a href="#__codelineno-11-68"> 68</a></span>
<span class="normal"><a href="#__codelineno-11-69"> 69</a></span>
<span class="normal"><a href="#__codelineno-11-70"> 70</a></span>
<span class="normal"><a href="#__codelineno-11-71"> 71</a></span>
<span class="normal"><a href="#__codelineno-11-72"> 72</a></span>
<span class="normal"><a href="#__codelineno-11-73"> 73</a></span>
<span class="normal"><a href="#__codelineno-11-74"> 74</a></span>
<span class="normal"><a href="#__codelineno-11-75"> 75</a></span>
<span class="normal"><a href="#__codelineno-11-76"> 76</a></span>
<span class="normal"><a href="#__codelineno-11-77"> 77</a></span>
<span class="normal"><a href="#__codelineno-11-78"> 78</a></span>
<span class="normal"><a href="#__codelineno-11-79"> 79</a></span>
<span class="normal"><a href="#__codelineno-11-80"> 80</a></span>
<span class="normal"><a href="#__codelineno-11-81"> 81</a></span>
<span class="normal"><a href="#__codelineno-11-82"> 82</a></span>
<span class="normal"><a href="#__codelineno-11-83"> 83</a></span>
<span class="normal"><a href="#__codelineno-11-84"> 84</a></span>
<span class="normal"><a href="#__codelineno-11-85"> 85</a></span>
<span class="normal"><a href="#__codelineno-11-86"> 86</a></span>
<span class="normal"><a href="#__codelineno-11-87"> 87</a></span>
<span class="normal"><a href="#__codelineno-11-88"> 88</a></span>
<span class="normal"><a href="#__codelineno-11-89"> 89</a></span>
<span class="normal"><a href="#__codelineno-11-90"> 90</a></span>
<span class="normal"><a href="#__codelineno-11-91"> 91</a></span>
<span class="normal"><a href="#__codelineno-11-92"> 92</a></span>
<span class="normal"><a href="#__codelineno-11-93"> 93</a></span>
<span class="normal"><a href="#__codelineno-11-94"> 94</a></span>
<span class="normal"><a href="#__codelineno-11-95"> 95</a></span>
<span class="normal"><a href="#__codelineno-11-96"> 96</a></span>
<span class="normal"><a href="#__codelineno-11-97"> 97</a></span>
<span class="normal"><a href="#__codelineno-11-98"> 98</a></span>
<span class="normal"><a href="#__codelineno-11-99"> 99</a></span>
<span class="normal"><a href="#__codelineno-11-100">100</a></span>
<span class="normal"><a href="#__codelineno-11-101">101</a></span>
<span class="normal"><a href="#__codelineno-11-102">102</a></span>
<span class="normal"><a href="#__codelineno-11-103">103</a></span>
<span class="normal"><a href="#__codelineno-11-104">104</a></span>
<span class="normal"><a href="#__codelineno-11-105">105</a></span>
<span class="normal"><a href="#__codelineno-11-106">106</a></span>
<span class="normal"><a href="#__codelineno-11-107">107</a></span>
<span class="normal"><a href="#__codelineno-11-108">108</a></span>
<span class="normal"><a href="#__codelineno-11-109">109</a></span>
<span class="normal"><a href="#__codelineno-11-110">110</a></span>
<span class="normal"><a href="#__codelineno-11-111">111</a></span>
<span class="normal"><a href="#__codelineno-11-112">112</a></span>
<span class="normal"><a href="#__codelineno-11-113">113</a></span>
<span class="normal"><a href="#__codelineno-11-114">114</a></span>
<span class="normal"><a href="#__codelineno-11-115">115</a></span>
<span class="normal"><a href="#__codelineno-11-116">116</a></span>
<span class="normal"><a href="#__codelineno-11-117">117</a></span>
<span class="normal"><a href="#__codelineno-11-118">118</a></span>
<span class="normal"><a href="#__codelineno-11-119">119</a></span>
<span class="normal"><a href="#__codelineno-11-120">120</a></span>
<span class="normal"><a href="#__codelineno-11-121">121</a></span>
<span class="normal"><a href="#__codelineno-11-122">122</a></span>
<span class="normal"><a href="#__codelineno-11-123">123</a></span>
<span class="normal"><a href="#__codelineno-11-124">124</a></span>
<span class="normal"><a href="#__codelineno-11-125">125</a></span>
<span class="normal"><a href="#__codelineno-11-126">126</a></span>
<span class="normal"><a href="#__codelineno-11-127">127</a></span>
<span class="normal"><a href="#__codelineno-11-128">128</a></span>
<span class="normal"><a href="#__codelineno-11-129">129</a></span>
<span class="normal"><a href="#__codelineno-11-130">130</a></span>
<span class="normal"><a href="#__codelineno-11-131">131</a></span>
<span class="normal"><a href="#__codelineno-11-132">132</a></span>
<span class="normal"><a href="#__codelineno-11-133">133</a></span>
<span class="normal"><a href="#__codelineno-11-134">134</a></span>
<span class="normal"><a href="#__codelineno-11-135">135</a></span>
<span class="normal"><a href="#__codelineno-11-136">136</a></span>
<span class="normal"><a href="#__codelineno-11-137">137</a></span>
<span class="normal"><a href="#__codelineno-11-138">138</a></span>
<span class="normal"><a href="#__codelineno-11-139">139</a></span>
<span class="normal"><a href="#__codelineno-11-140">140</a></span>
<span class="normal"><a href="#__codelineno-11-141">141</a></span>
<span class="normal"><a href="#__codelineno-11-142">142</a></span>
<span class="normal"><a href="#__codelineno-11-143">143</a></span>
<span class="normal"><a href="#__codelineno-11-144">144</a></span>
<span class="normal"><a href="#__codelineno-11-145">145</a></span>
<span class="normal"><a href="#__codelineno-11-146">146</a></span>
<span class="normal"><a href="#__codelineno-11-147">147</a></span>
<span class="normal"><a href="#__codelineno-11-148">148</a></span>
<span class="normal"><a href="#__codelineno-11-149">149</a></span>
<span class="normal"><a href="#__codelineno-11-150">150</a></span>
<span class="normal"><a href="#__codelineno-11-151">151</a></span>
<span class="normal"><a href="#__codelineno-11-152">152</a></span>
<span class="normal"><a href="#__codelineno-11-153">153</a></span>
<span class="normal"><a href="#__codelineno-11-154">154</a></span>
<span class="normal"><a href="#__codelineno-11-155">155</a></span>
<span class="normal"><a href="#__codelineno-11-156">156</a></span>
<span class="normal"><a href="#__codelineno-11-157">157</a></span>
<span class="normal"><a href="#__codelineno-11-158">158</a></span>
<span class="normal"><a href="#__codelineno-11-159">159</a></span>
<span class="normal"><a href="#__codelineno-11-160">160</a></span>
<span class="normal"><a href="#__codelineno-11-161">161</a></span>
<span class="normal"><a href="#__codelineno-11-162">162</a></span>
<span class="normal"><a href="#__codelineno-11-163">163</a></span>
<span class="normal"><a href="#__codelineno-11-164">164</a></span>
<span class="normal"><a href="#__codelineno-11-165">165</a></span>
<span class="normal"><a href="#__codelineno-11-166">166</a></span>
<span class="normal"><a href="#__codelineno-11-167">167</a></span>
<span class="normal"><a href="#__codelineno-11-168">168</a></span>
<span class="normal"><a href="#__codelineno-11-169">169</a></span>
<span class="normal"><a href="#__codelineno-11-170">170</a></span>
<span class="normal"><a href="#__codelineno-11-171">171</a></span>
<span class="normal"><a href="#__codelineno-11-172">172</a></span>
<span class="normal"><a href="#__codelineno-11-173">173</a></span>
<span class="normal"><a href="#__codelineno-11-174">174</a></span>
<span class="normal"><a href="#__codelineno-11-175">175</a></span>
<span class="normal"><a href="#__codelineno-11-176">176</a></span>
<span class="normal"><a href="#__codelineno-11-177">177</a></span>
<span class="normal"><a href="#__codelineno-11-178">178</a></span>
<span class="normal"><a href="#__codelineno-11-179">179</a></span>
<span class="normal"><a href="#__codelineno-11-180">180</a></span>
<span class="normal"><a href="#__codelineno-11-181">181</a></span>
<span class="normal"><a href="#__codelineno-11-182">182</a></span>
<span class="normal"><a href="#__codelineno-11-183">183</a></span>
<span class="normal"><a href="#__codelineno-11-184">184</a></span>
<span class="normal"><a href="#__codelineno-11-185">185</a></span>
<span class="normal"><a href="#__codelineno-11-186">186</a></span>
<span class="normal"><a href="#__codelineno-11-187">187</a></span>
<span class="normal"><a href="#__codelineno-11-188">188</a></span>
<span class="normal"><a href="#__codelineno-11-189">189</a></span>
<span class="normal"><a href="#__codelineno-11-190">190</a></span>
<span class="normal"><a href="#__codelineno-11-191">191</a></span>
<span class="normal"><a href="#__codelineno-11-192">192</a></span>
<span class="normal"><a href="#__codelineno-11-193">193</a></span>
<span class="normal"><a href="#__codelineno-11-194">194</a></span>
<span class="normal"><a href="#__codelineno-11-195">195</a></span>
<span class="normal"><a href="#__codelineno-11-196">196</a></span>
<span class="normal"><a href="#__codelineno-11-197">197</a></span>
<span class="normal"><a href="#__codelineno-11-198">198</a></span>
<span class="normal"><a href="#__codelineno-11-199">199</a></span>
<span class="normal"><a href="#__codelineno-11-200">200</a></span>
<span class="normal"><a href="#__codelineno-11-201">201</a></span>
<span class="normal"><a href="#__codelineno-11-202">202</a></span>
<span class="normal"><a href="#__codelineno-11-203">203</a></span>
<span class="normal"><a href="#__codelineno-11-204">204</a></span>
<span class="normal"><a href="#__codelineno-11-205">205</a></span>
<span class="normal"><a href="#__codelineno-11-206">206</a></span>
<span class="normal"><a href="#__codelineno-11-207">207</a></span>
<span class="normal"><a href="#__codelineno-11-208">208</a></span>
<span class="normal"><a href="#__codelineno-11-209">209</a></span>
<span class="normal"><a href="#__codelineno-11-210">210</a></span>
<span class="normal"><a href="#__codelineno-11-211">211</a></span>
<span class="normal"><a href="#__codelineno-11-212">212</a></span>
<span class="normal"><a href="#__codelineno-11-213">213</a></span>
<span class="normal"><a href="#__codelineno-11-214">214</a></span>
<span class="normal"><a href="#__codelineno-11-215">215</a></span>
<span class="normal"><a href="#__codelineno-11-216">216</a></span>
<span class="normal"><a href="#__codelineno-11-217">217</a></span>
<span class="normal"><a href="#__codelineno-11-218">218</a></span>
<span class="normal"><a href="#__codelineno-11-219">219</a></span>
<span class="normal"><a href="#__codelineno-11-220">220</a></span>
<span class="normal"><a href="#__codelineno-11-221">221</a></span>
<span class="normal"><a href="#__codelineno-11-222">222</a></span>
<span class="normal"><a href="#__codelineno-11-223">223</a></span>
<span class="normal"><a href="#__codelineno-11-224">224</a></span>
<span class="normal"><a href="#__codelineno-11-225">225</a></span>
<span class="normal"><a href="#__codelineno-11-226">226</a></span>
<span class="normal"><a href="#__codelineno-11-227">227</a></span>
<span class="normal"><a href="#__codelineno-11-228">228</a></span>
<span class="normal"><a href="#__codelineno-11-229">229</a></span>
<span class="normal"><a href="#__codelineno-11-230">230</a></span>
<span class="normal"><a href="#__codelineno-11-231">231</a></span>
<span class="normal"><a href="#__codelineno-11-232">232</a></span>
<span class="normal"><a href="#__codelineno-11-233">233</a></span>
<span class="normal"><a href="#__codelineno-11-234">234</a></span>
<span class="normal"><a href="#__codelineno-11-235">235</a></span>
<span class="normal"><a href="#__codelineno-11-236">236</a></span>
<span class="normal"><a href="#__codelineno-11-237">237</a></span>
<span class="normal"><a href="#__codelineno-11-238">238</a></span>
<span class="normal"><a href="#__codelineno-11-239">239</a></span>
<span class="normal"><a href="#__codelineno-11-240">240</a></span>
<span class="normal"><a href="#__codelineno-11-241">241</a></span>
<span class="normal"><a href="#__codelineno-11-242">242</a></span>
<span class="normal"><a href="#__codelineno-11-243">243</a></span>
<span class="normal"><a href="#__codelineno-11-244">244</a></span>
<span class="normal"><a href="#__codelineno-11-245">245</a></span>
<span class="normal"><a href="#__codelineno-11-246">246</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu.demo.dao</span><span class="p">;</span>
<a id="__codelineno-11-2" name="__codelineno-11-2"></a>
<a id="__codelineno-11-3" name="__codelineno-11-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.sql.*</span><span class="p">;</span>
<a id="__codelineno-11-4" name="__codelineno-11-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.util.ArrayList</span><span class="p">;</span>
<a id="__codelineno-11-5" name="__codelineno-11-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.Result</span><span class="p">;</span>
<a id="__codelineno-11-6" name="__codelineno-11-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.ConnectionUtil</span><span class="p">;</span>
<a id="__codelineno-11-7" name="__codelineno-11-7"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.domain.Student</span><span class="p">;</span>
<a id="__codelineno-11-8" name="__codelineno-11-8"></a>
<a id="__codelineno-11-9" name="__codelineno-11-9"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">StudentDaoImpl</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">StudentDao</span><span class="p">{</span>
<a id="__codelineno-11-10" name="__codelineno-11-10"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">init</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-11" name="__codelineno-11-11"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-11-12" name="__codelineno-11-12"></a>
<a id="__codelineno-11-13" name="__codelineno-11-13"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-14" name="__codelineno-11-14"></a><span class="w">    </span><span class="n">Statement</span><span class="w"> </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-15" name="__codelineno-11-15"></a><span class="w">    </span><span class="n">String</span><span class="w"> </span><span class="n">sql</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;create table Student(&quot;</span><span class="w"> </span><span class="o">+</span>
<a id="__codelineno-11-16" name="__codelineno-11-16"></a><span class="w">      </span><span class="s">&quot;no TEXT primary key,&quot;</span><span class="w"> </span><span class="o">+</span>
<a id="__codelineno-11-17" name="__codelineno-11-17"></a><span class="w">      </span><span class="s">&quot;name TEXT not null,&quot;</span><span class="w"> </span><span class="o">+</span>
<a id="__codelineno-11-18" name="__codelineno-11-18"></a><span class="w">      </span><span class="s">&quot;gender TEXT,&quot;</span><span class="w"> </span><span class="o">+</span>
<a id="__codelineno-11-19" name="__codelineno-11-19"></a><span class="w">      </span><span class="s">&quot;age INTEGER,&quot;</span><span class="w"> </span><span class="o">+</span>
<a id="__codelineno-11-20" name="__codelineno-11-20"></a><span class="w">      </span><span class="s">&quot;dept TEXT&quot;</span><span class="w"> </span><span class="o">+</span>
<a id="__codelineno-11-21" name="__codelineno-11-21"></a><span class="w">      </span><span class="s">&quot;);&quot;</span><span class="p">;</span>
<a id="__codelineno-11-22" name="__codelineno-11-22"></a>
<a id="__codelineno-11-23" name="__codelineno-11-23"></a><span class="w">    </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">getConnection</span><span class="p">();</span>
<a id="__codelineno-11-24" name="__codelineno-11-24"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">con</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-25" name="__codelineno-11-25"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-26" name="__codelineno-11-26"></a><span class="w">        </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="na">createStatement</span><span class="p">();</span>
<a id="__codelineno-11-27" name="__codelineno-11-27"></a><span class="w">        </span><span class="n">ResultSet</span><span class="w"> </span><span class="n">rs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stmt</span><span class="p">.</span><span class="na">executeQuery</span><span class="p">(</span><span class="n">String</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="s">&quot;select count(*) from sqlite_master where type = &#39;table&#39; and Upper(name) = &#39;%s&#39;&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Student&quot;</span><span class="p">.</span><span class="na">toUpperCase</span><span class="p">()));</span>
<a id="__codelineno-11-28" name="__codelineno-11-28"></a><span class="w">        </span><span class="k">while</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">next</span><span class="p">()){</span>
<a id="__codelineno-11-29" name="__codelineno-11-29"></a><span class="w">          </span><span class="kt">int</span><span class="w"> </span><span class="n">count</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rs</span><span class="p">.</span><span class="na">getInt</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<a id="__codelineno-11-30" name="__codelineno-11-30"></a>
<a id="__codelineno-11-31" name="__codelineno-11-31"></a><span class="w">          </span><span class="k">if</span><span class="p">(</span><span class="n">count</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">stmt</span><span class="p">.</span><span class="na">executeUpdate</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">){</span>
<a id="__codelineno-11-32" name="__codelineno-11-32"></a><span class="w">            </span><span class="n">res</span><span class="p">.</span><span class="na">code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<a id="__codelineno-11-33" name="__codelineno-11-33"></a><span class="w">          </span><span class="p">}</span><span class="w">          </span>
<a id="__codelineno-11-34" name="__codelineno-11-34"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-11-35" name="__codelineno-11-35"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-36" name="__codelineno-11-36"></a><span class="w">        </span><span class="n">e</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">();</span>
<a id="__codelineno-11-37" name="__codelineno-11-37"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">();</span>
<a id="__codelineno-11-38" name="__codelineno-11-38"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">finally</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-39" name="__codelineno-11-39"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closeStmt</span><span class="p">(</span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-40" name="__codelineno-11-40"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closeConnection</span><span class="p">(</span><span class="n">con</span><span class="p">);</span>
<a id="__codelineno-11-41" name="__codelineno-11-41"></a><span class="w">      </span><span class="p">}</span><span class="w">      </span>
<a id="__codelineno-11-42" name="__codelineno-11-42"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-43" name="__codelineno-11-43"></a>
<a id="__codelineno-11-44" name="__codelineno-11-44"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-11-45" name="__codelineno-11-45"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-46" name="__codelineno-11-46"></a>
<a id="__codelineno-11-47" name="__codelineno-11-47"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-11-48" name="__codelineno-11-48"></a><span class="cm">   * 对象填充到关系</span>
<a id="__codelineno-11-49" name="__codelineno-11-49"></a><span class="cm">   */</span>
<a id="__codelineno-11-50" name="__codelineno-11-50"></a><span class="w">  </span><span class="kd">private</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">toR</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">,</span><span class="w"> </span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">stmt</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">Exception</span><span class="p">{</span>
<a id="__codelineno-11-51" name="__codelineno-11-51"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">o</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">stmt</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-52" name="__codelineno-11-52"></a><span class="w">      </span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="na">getNo</span><span class="p">();</span>
<a id="__codelineno-11-53" name="__codelineno-11-53"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">no</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-54" name="__codelineno-11-54"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setString</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">no</span><span class="p">);</span><span class="w">       </span>
<a id="__codelineno-11-55" name="__codelineno-11-55"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-11-56" name="__codelineno-11-56"></a><span class="w">      </span><span class="n">String</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="na">getName</span><span class="p">();</span>
<a id="__codelineno-11-57" name="__codelineno-11-57"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">name</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-58" name="__codelineno-11-58"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setString</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="n">name</span><span class="p">);</span><span class="w">       </span>
<a id="__codelineno-11-59" name="__codelineno-11-59"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-11-60" name="__codelineno-11-60"></a><span class="w">      </span><span class="n">String</span><span class="w"> </span><span class="n">gender</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="na">getGender</span><span class="p">();</span>
<a id="__codelineno-11-61" name="__codelineno-11-61"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">gender</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-62" name="__codelineno-11-62"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setString</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">gender</span><span class="p">);</span><span class="w">       </span>
<a id="__codelineno-11-63" name="__codelineno-11-63"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-11-64" name="__codelineno-11-64"></a><span class="w">      </span><span class="n">Integer</span><span class="w"> </span><span class="n">age</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="na">getAge</span><span class="p">();</span>
<a id="__codelineno-11-65" name="__codelineno-11-65"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">age</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-66" name="__codelineno-11-66"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setInt</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="n">age</span><span class="p">);</span><span class="w">       </span>
<a id="__codelineno-11-67" name="__codelineno-11-67"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-11-68" name="__codelineno-11-68"></a><span class="w">      </span><span class="n">String</span><span class="w"> </span><span class="n">dept</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="na">getDept</span><span class="p">();</span>
<a id="__codelineno-11-69" name="__codelineno-11-69"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">dept</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-70" name="__codelineno-11-70"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setString</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="n">dept</span><span class="p">);</span><span class="w">       </span>
<a id="__codelineno-11-71" name="__codelineno-11-71"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-11-72" name="__codelineno-11-72"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-73" name="__codelineno-11-73"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-74" name="__codelineno-11-74"></a>
<a id="__codelineno-11-75" name="__codelineno-11-75"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-11-76" name="__codelineno-11-76"></a><span class="cm">   * 关系填充到对象</span>
<a id="__codelineno-11-77" name="__codelineno-11-77"></a><span class="cm">   */</span>
<a id="__codelineno-11-78" name="__codelineno-11-78"></a><span class="w">  </span><span class="kd">private</span><span class="w"> </span><span class="n">Student</span><span class="w"> </span><span class="nf">toO</span><span class="p">(</span><span class="n">ResultSet</span><span class="w"> </span><span class="n">rs</span><span class="p">)</span><span class="w"> </span><span class="kd">throws</span><span class="w"> </span><span class="n">Exception</span><span class="p">{</span>
<a id="__codelineno-11-79" name="__codelineno-11-79"></a><span class="w">    </span><span class="n">Student</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-80" name="__codelineno-11-80"></a>
<a id="__codelineno-11-81" name="__codelineno-11-81"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">rs</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-82" name="__codelineno-11-82"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Student</span><span class="p">();</span>
<a id="__codelineno-11-83" name="__codelineno-11-83"></a><span class="w">      </span><span class="n">res</span><span class="p">.</span><span class="na">setNo</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;no&quot;</span><span class="p">));</span>
<a id="__codelineno-11-84" name="__codelineno-11-84"></a><span class="w">      </span><span class="n">res</span><span class="p">.</span><span class="na">setName</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">));</span>
<a id="__codelineno-11-85" name="__codelineno-11-85"></a><span class="w">      </span><span class="n">res</span><span class="p">.</span><span class="na">setGender</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;gender&quot;</span><span class="p">));</span>
<a id="__codelineno-11-86" name="__codelineno-11-86"></a><span class="w">      </span><span class="n">res</span><span class="p">.</span><span class="na">setAge</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getInt</span><span class="p">(</span><span class="s">&quot;age&quot;</span><span class="p">));</span>
<a id="__codelineno-11-87" name="__codelineno-11-87"></a><span class="w">      </span><span class="n">res</span><span class="p">.</span><span class="na">setDept</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;dept&quot;</span><span class="p">));</span>
<a id="__codelineno-11-88" name="__codelineno-11-88"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-89" name="__codelineno-11-89"></a>
<a id="__codelineno-11-90" name="__codelineno-11-90"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-11-91" name="__codelineno-11-91"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-92" name="__codelineno-11-92"></a>
<a id="__codelineno-11-93" name="__codelineno-11-93"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">create</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-94" name="__codelineno-11-94"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-11-95" name="__codelineno-11-95"></a>
<a id="__codelineno-11-96" name="__codelineno-11-96"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-97" name="__codelineno-11-97"></a><span class="w">    </span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-98" name="__codelineno-11-98"></a><span class="w">    </span><span class="n">String</span><span class="w"> </span><span class="n">sql</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;insert into Student values (?, ?, ?, ?, ?)&quot;</span><span class="p">;</span>
<a id="__codelineno-11-99" name="__codelineno-11-99"></a><span class="w">    </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">getConnection</span><span class="p">();</span>
<a id="__codelineno-11-100" name="__codelineno-11-100"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">o</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-101" name="__codelineno-11-101"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-102" name="__codelineno-11-102"></a><span class="w">        </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="na">prepareStatement</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>
<a id="__codelineno-11-103" name="__codelineno-11-103"></a><span class="w">        </span><span class="n">toR</span><span class="p">(</span><span class="n">o</span><span class="p">,</span><span class="w"> </span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-104" name="__codelineno-11-104"></a><span class="w">        </span><span class="kt">int</span><span class="w"> </span><span class="n">row_effected</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stmt</span><span class="p">.</span><span class="na">executeUpdate</span><span class="p">();</span>
<a id="__codelineno-11-105" name="__codelineno-11-105"></a><span class="w">        </span><span class="k">if</span><span class="p">(</span><span class="n">row_effected</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-106" name="__codelineno-11-106"></a><span class="w">            </span><span class="n">res</span><span class="p">.</span><span class="na">code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<a id="__codelineno-11-107" name="__codelineno-11-107"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-11-108" name="__codelineno-11-108"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">row_effected</span><span class="p">;</span>
<a id="__codelineno-11-109" name="__codelineno-11-109"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-110" name="__codelineno-11-110"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">();</span>
<a id="__codelineno-11-111" name="__codelineno-11-111"></a><span class="w">        </span><span class="n">e</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">();</span>
<a id="__codelineno-11-112" name="__codelineno-11-112"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">finally</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-113" name="__codelineno-11-113"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closePstmt</span><span class="p">(</span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-114" name="__codelineno-11-114"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closeConnection</span><span class="p">(</span><span class="n">con</span><span class="p">);</span>
<a id="__codelineno-11-115" name="__codelineno-11-115"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-11-116" name="__codelineno-11-116"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-117" name="__codelineno-11-117"></a>
<a id="__codelineno-11-118" name="__codelineno-11-118"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-11-119" name="__codelineno-11-119"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-120" name="__codelineno-11-120"></a>
<a id="__codelineno-11-121" name="__codelineno-11-121"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">delete</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-122" name="__codelineno-11-122"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-11-123" name="__codelineno-11-123"></a>
<a id="__codelineno-11-124" name="__codelineno-11-124"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-125" name="__codelineno-11-125"></a><span class="w">    </span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-126" name="__codelineno-11-126"></a><span class="w">    </span><span class="n">String</span><span class="w"> </span><span class="n">sql</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;delete from Student where no = ?;&quot;</span><span class="p">;</span>
<a id="__codelineno-11-127" name="__codelineno-11-127"></a><span class="w">    </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">getConnection</span><span class="p">();</span>
<a id="__codelineno-11-128" name="__codelineno-11-128"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">con</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-129" name="__codelineno-11-129"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-130" name="__codelineno-11-130"></a>
<a id="__codelineno-11-131" name="__codelineno-11-131"></a><span class="w">        </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="na">prepareStatement</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>
<a id="__codelineno-11-132" name="__codelineno-11-132"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setString</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-11-133" name="__codelineno-11-133"></a><span class="w">        </span><span class="kt">int</span><span class="w"> </span><span class="n">row_effected</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stmt</span><span class="p">.</span><span class="na">executeUpdate</span><span class="p">();</span>
<a id="__codelineno-11-134" name="__codelineno-11-134"></a><span class="w">        </span><span class="k">if</span><span class="p">(</span><span class="n">row_effected</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-135" name="__codelineno-11-135"></a><span class="w">            </span><span class="n">res</span><span class="p">.</span><span class="na">code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<a id="__codelineno-11-136" name="__codelineno-11-136"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-11-137" name="__codelineno-11-137"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">row_effected</span><span class="p">;</span>
<a id="__codelineno-11-138" name="__codelineno-11-138"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-139" name="__codelineno-11-139"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">();</span>
<a id="__codelineno-11-140" name="__codelineno-11-140"></a><span class="w">        </span><span class="n">e</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">();</span>
<a id="__codelineno-11-141" name="__codelineno-11-141"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">finally</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-142" name="__codelineno-11-142"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closePstmt</span><span class="p">(</span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-143" name="__codelineno-11-143"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closeConnection</span><span class="p">(</span><span class="n">con</span><span class="p">);</span>
<a id="__codelineno-11-144" name="__codelineno-11-144"></a><span class="w">      </span><span class="p">}</span><span class="w">      </span>
<a id="__codelineno-11-145" name="__codelineno-11-145"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-146" name="__codelineno-11-146"></a>
<a id="__codelineno-11-147" name="__codelineno-11-147"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-11-148" name="__codelineno-11-148"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-149" name="__codelineno-11-149"></a>
<a id="__codelineno-11-150" name="__codelineno-11-150"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">update</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-151" name="__codelineno-11-151"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-11-152" name="__codelineno-11-152"></a>
<a id="__codelineno-11-153" name="__codelineno-11-153"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-154" name="__codelineno-11-154"></a><span class="w">    </span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-155" name="__codelineno-11-155"></a><span class="w">    </span><span class="n">String</span><span class="w"> </span><span class="n">sql</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;update Student set No = ?, Name = ?, Gender = ?, Age = ?, Dept = ? where no = ?&quot;</span><span class="p">;</span>
<a id="__codelineno-11-156" name="__codelineno-11-156"></a><span class="w">    </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">getConnection</span><span class="p">();</span>
<a id="__codelineno-11-157" name="__codelineno-11-157"></a><span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">o</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-158" name="__codelineno-11-158"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-159" name="__codelineno-11-159"></a>
<a id="__codelineno-11-160" name="__codelineno-11-160"></a><span class="w">        </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="na">prepareStatement</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>
<a id="__codelineno-11-161" name="__codelineno-11-161"></a><span class="w">        </span><span class="n">toR</span><span class="p">(</span><span class="n">o</span><span class="p">,</span><span class="w"> </span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-162" name="__codelineno-11-162"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setString</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="n">o</span><span class="p">.</span><span class="na">getNo</span><span class="p">());</span>
<a id="__codelineno-11-163" name="__codelineno-11-163"></a><span class="w">        </span><span class="kt">int</span><span class="w"> </span><span class="n">row_effected</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stmt</span><span class="p">.</span><span class="na">executeUpdate</span><span class="p">();</span>
<a id="__codelineno-11-164" name="__codelineno-11-164"></a><span class="w">        </span><span class="k">if</span><span class="p">(</span><span class="n">row_effected</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-165" name="__codelineno-11-165"></a><span class="w">            </span><span class="n">res</span><span class="p">.</span><span class="na">code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<a id="__codelineno-11-166" name="__codelineno-11-166"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-11-167" name="__codelineno-11-167"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">row_effected</span><span class="p">;</span>
<a id="__codelineno-11-168" name="__codelineno-11-168"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-169" name="__codelineno-11-169"></a><span class="w">        </span><span class="n">e</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">();</span>
<a id="__codelineno-11-170" name="__codelineno-11-170"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">();</span>
<a id="__codelineno-11-171" name="__codelineno-11-171"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">finally</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-172" name="__codelineno-11-172"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closePstmt</span><span class="p">(</span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-173" name="__codelineno-11-173"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closeConnection</span><span class="p">(</span><span class="n">con</span><span class="p">);</span>
<a id="__codelineno-11-174" name="__codelineno-11-174"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-11-175" name="__codelineno-11-175"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-176" name="__codelineno-11-176"></a>
<a id="__codelineno-11-177" name="__codelineno-11-177"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-11-178" name="__codelineno-11-178"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-179" name="__codelineno-11-179"></a>
<a id="__codelineno-11-180" name="__codelineno-11-180"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">get</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-181" name="__codelineno-11-181"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-11-182" name="__codelineno-11-182"></a>
<a id="__codelineno-11-183" name="__codelineno-11-183"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-184" name="__codelineno-11-184"></a><span class="w">    </span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-185" name="__codelineno-11-185"></a><span class="w">    </span><span class="n">String</span><span class="w"> </span><span class="n">sql</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;select * from Student where no = ?&quot;</span><span class="p">;</span>
<a id="__codelineno-11-186" name="__codelineno-11-186"></a><span class="w">    </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">getConnection</span><span class="p">();</span>
<a id="__codelineno-11-187" name="__codelineno-11-187"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">con</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-188" name="__codelineno-11-188"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-189" name="__codelineno-11-189"></a><span class="w">        </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="na">prepareStatement</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>
<a id="__codelineno-11-190" name="__codelineno-11-190"></a><span class="w">        </span><span class="n">stmt</span><span class="p">.</span><span class="na">setString</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-11-191" name="__codelineno-11-191"></a><span class="w">        </span><span class="n">ResultSet</span><span class="w"> </span><span class="n">rs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stmt</span><span class="p">.</span><span class="na">executeQuery</span><span class="p">();</span>
<a id="__codelineno-11-192" name="__codelineno-11-192"></a><span class="w">        </span><span class="n">Student</span><span class="w"> </span><span class="n">last</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-193" name="__codelineno-11-193"></a><span class="w">        </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">next</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-194" name="__codelineno-11-194"></a><span class="w">          </span><span class="n">last</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Student</span><span class="p">();</span>
<a id="__codelineno-11-195" name="__codelineno-11-195"></a><span class="w">          </span><span class="n">last</span><span class="p">.</span><span class="na">setNo</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;no&quot;</span><span class="p">));</span>
<a id="__codelineno-11-196" name="__codelineno-11-196"></a><span class="w">          </span><span class="n">last</span><span class="p">.</span><span class="na">setName</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">));</span>
<a id="__codelineno-11-197" name="__codelineno-11-197"></a><span class="w">          </span><span class="n">last</span><span class="p">.</span><span class="na">setGender</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;gender&quot;</span><span class="p">));</span>
<a id="__codelineno-11-198" name="__codelineno-11-198"></a><span class="w">          </span><span class="n">last</span><span class="p">.</span><span class="na">setAge</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getInt</span><span class="p">(</span><span class="s">&quot;age&quot;</span><span class="p">));</span>
<a id="__codelineno-11-199" name="__codelineno-11-199"></a><span class="w">          </span><span class="n">last</span><span class="p">.</span><span class="na">setDept</span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">getString</span><span class="p">(</span><span class="s">&quot;dept&quot;</span><span class="p">));</span>
<a id="__codelineno-11-200" name="__codelineno-11-200"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-11-201" name="__codelineno-11-201"></a>
<a id="__codelineno-11-202" name="__codelineno-11-202"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<a id="__codelineno-11-203" name="__codelineno-11-203"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">last</span><span class="p">;</span>
<a id="__codelineno-11-204" name="__codelineno-11-204"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-205" name="__codelineno-11-205"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">();</span>
<a id="__codelineno-11-206" name="__codelineno-11-206"></a><span class="w">        </span><span class="n">e</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">();</span>
<a id="__codelineno-11-207" name="__codelineno-11-207"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">finally</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-208" name="__codelineno-11-208"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closePstmt</span><span class="p">(</span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-209" name="__codelineno-11-209"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closeConnection</span><span class="p">(</span><span class="n">con</span><span class="p">);</span>
<a id="__codelineno-11-210" name="__codelineno-11-210"></a><span class="w">      </span><span class="p">}</span><span class="w">      </span>
<a id="__codelineno-11-211" name="__codelineno-11-211"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-212" name="__codelineno-11-212"></a>
<a id="__codelineno-11-213" name="__codelineno-11-213"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-11-214" name="__codelineno-11-214"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-215" name="__codelineno-11-215"></a>
<a id="__codelineno-11-216" name="__codelineno-11-216"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">getAll</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-217" name="__codelineno-11-217"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-11-218" name="__codelineno-11-218"></a>
<a id="__codelineno-11-219" name="__codelineno-11-219"></a><span class="w">    </span><span class="n">Connection</span><span class="w"> </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-220" name="__codelineno-11-220"></a><span class="w">    </span><span class="n">PreparedStatement</span><span class="w"> </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-11-221" name="__codelineno-11-221"></a><span class="w">    </span><span class="n">String</span><span class="w"> </span><span class="n">sql</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;select * from Student&quot;</span><span class="p">;</span>
<a id="__codelineno-11-222" name="__codelineno-11-222"></a><span class="w">    </span><span class="n">con</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">getConnection</span><span class="p">();</span>
<a id="__codelineno-11-223" name="__codelineno-11-223"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">con</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-11-224" name="__codelineno-11-224"></a><span class="w">      </span><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-225" name="__codelineno-11-225"></a><span class="w">        </span><span class="n">stmt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">con</span><span class="p">.</span><span class="na">prepareStatement</span><span class="p">(</span><span class="n">sql</span><span class="p">);</span>
<a id="__codelineno-11-226" name="__codelineno-11-226"></a><span class="w">        </span><span class="n">ResultSet</span><span class="w"> </span><span class="n">rs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stmt</span><span class="p">.</span><span class="na">executeQuery</span><span class="p">();</span>
<a id="__codelineno-11-227" name="__codelineno-11-227"></a><span class="w">        </span><span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">Student</span><span class="o">&gt;</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">Student</span><span class="o">&gt;</span><span class="p">();</span>
<a id="__codelineno-11-228" name="__codelineno-11-228"></a><span class="w">        </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">rs</span><span class="p">.</span><span class="na">next</span><span class="p">())</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-229" name="__codelineno-11-229"></a><span class="w">          </span><span class="n">Student</span><span class="w"> </span><span class="n">last</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">toO</span><span class="p">(</span><span class="n">rs</span><span class="p">);</span>
<a id="__codelineno-11-230" name="__codelineno-11-230"></a><span class="w">          </span><span class="n">data</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="n">last</span><span class="p">);</span>
<a id="__codelineno-11-231" name="__codelineno-11-231"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-11-232" name="__codelineno-11-232"></a>
<a id="__codelineno-11-233" name="__codelineno-11-233"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span>
<a id="__codelineno-11-234" name="__codelineno-11-234"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">data</span><span class="p">;</span>
<a id="__codelineno-11-235" name="__codelineno-11-235"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-236" name="__codelineno-11-236"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">e</span><span class="p">.</span><span class="na">getMessage</span><span class="p">();</span>
<a id="__codelineno-11-237" name="__codelineno-11-237"></a><span class="w">        </span><span class="n">e</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">();</span>
<a id="__codelineno-11-238" name="__codelineno-11-238"></a><span class="w">      </span><span class="p">}</span><span class="w"> </span><span class="k">finally</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-11-239" name="__codelineno-11-239"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closePstmt</span><span class="p">(</span><span class="n">stmt</span><span class="p">);</span>
<a id="__codelineno-11-240" name="__codelineno-11-240"></a><span class="w">        </span><span class="n">ConnectionUtil</span><span class="p">.</span><span class="na">closeConnection</span><span class="p">(</span><span class="n">con</span><span class="p">);</span>
<a id="__codelineno-11-241" name="__codelineno-11-241"></a><span class="w">      </span><span class="p">}</span><span class="w">      </span>
<a id="__codelineno-11-242" name="__codelineno-11-242"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-11-243" name="__codelineno-11-243"></a>
<a id="__codelineno-11-244" name="__codelineno-11-244"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-11-245" name="__codelineno-11-245"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-11-246" name="__codelineno-11-246"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>创建服务对象<code>StudentService.java</code>和<code>StudentServiceImpl.java</code>，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/src/main/java/xust/stu/demo/service/StudentService.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-12-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-12-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-12-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-12-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-12-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-12-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-12-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-12-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-12-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-12-10">10</a></span>
<span class="normal"><a href="#__codelineno-12-11">11</a></span>
<span class="normal"><a href="#__codelineno-12-12">12</a></span>
<span class="normal"><a href="#__codelineno-12-13">13</a></span>
<span class="normal"><a href="#__codelineno-12-14">14</a></span>
<span class="normal"><a href="#__codelineno-12-15">15</a></span>
<span class="normal"><a href="#__codelineno-12-16">16</a></span>
<span class="normal"><a href="#__codelineno-12-17">17</a></span>
<span class="normal"><a href="#__codelineno-12-18">18</a></span>
<span class="normal"><a href="#__codelineno-12-19">19</a></span>
<span class="normal"><a href="#__codelineno-12-20">20</a></span>
<span class="normal"><a href="#__codelineno-12-21">21</a></span>
<span class="normal"><a href="#__codelineno-12-22">22</a></span>
<span class="normal"><a href="#__codelineno-12-23">23</a></span>
<span class="normal"><a href="#__codelineno-12-24">24</a></span>
<span class="normal"><a href="#__codelineno-12-25">25</a></span>
<span class="normal"><a href="#__codelineno-12-26">26</a></span>
<span class="normal"><a href="#__codelineno-12-27">27</a></span>
<span class="normal"><a href="#__codelineno-12-28">28</a></span>
<span class="normal"><a href="#__codelineno-12-29">29</a></span>
<span class="normal"><a href="#__codelineno-12-30">30</a></span>
<span class="normal"><a href="#__codelineno-12-31">31</a></span>
<span class="normal"><a href="#__codelineno-12-32">32</a></span>
<span class="normal"><a href="#__codelineno-12-33">33</a></span>
<span class="normal"><a href="#__codelineno-12-34">34</a></span>
<span class="normal"><a href="#__codelineno-12-35">35</a></span>
<span class="normal"><a href="#__codelineno-12-36">36</a></span>
<span class="normal"><a href="#__codelineno-12-37">37</a></span>
<span class="normal"><a href="#__codelineno-12-38">38</a></span>
<span class="normal"><a href="#__codelineno-12-39">39</a></span>
<span class="normal"><a href="#__codelineno-12-40">40</a></span>
<span class="normal"><a href="#__codelineno-12-41">41</a></span>
<span class="normal"><a href="#__codelineno-12-42">42</a></span>
<span class="normal"><a href="#__codelineno-12-43">43</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu.demo.service</span><span class="p">;</span>
<a id="__codelineno-12-2" name="__codelineno-12-2"></a>
<a id="__codelineno-12-3" name="__codelineno-12-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.Result</span><span class="p">;</span>
<a id="__codelineno-12-4" name="__codelineno-12-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.domain.Student</span><span class="p">;</span>
<a id="__codelineno-12-5" name="__codelineno-12-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletRequest</span><span class="p">;</span>
<a id="__codelineno-12-6" name="__codelineno-12-6"></a>
<a id="__codelineno-12-7" name="__codelineno-12-7"></a><span class="kd">public</span><span class="w"> </span><span class="kd">interface</span> <span class="nc">StudentService</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-12-8" name="__codelineno-12-8"></a><span class="w">  </span><span class="n">Student</span><span class="w"> </span><span class="nf">toO</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">);</span>
<a id="__codelineno-12-9" name="__codelineno-12-9"></a>
<a id="__codelineno-12-10" name="__codelineno-12-10"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-12-11" name="__codelineno-12-11"></a><span class="cm">   * 增</span>
<a id="__codelineno-12-12" name="__codelineno-12-12"></a><span class="cm">   * @param o</span>
<a id="__codelineno-12-13" name="__codelineno-12-13"></a><span class="cm">   * @return</span>
<a id="__codelineno-12-14" name="__codelineno-12-14"></a><span class="cm">   */</span>
<a id="__codelineno-12-15" name="__codelineno-12-15"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">create</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">);</span>
<a id="__codelineno-12-16" name="__codelineno-12-16"></a>
<a id="__codelineno-12-17" name="__codelineno-12-17"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-12-18" name="__codelineno-12-18"></a><span class="cm">   * 删</span>
<a id="__codelineno-12-19" name="__codelineno-12-19"></a><span class="cm">   * @param id</span>
<a id="__codelineno-12-20" name="__codelineno-12-20"></a><span class="cm">   * @return</span>
<a id="__codelineno-12-21" name="__codelineno-12-21"></a><span class="cm">   */</span>
<a id="__codelineno-12-22" name="__codelineno-12-22"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">delete</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-12-23" name="__codelineno-12-23"></a>
<a id="__codelineno-12-24" name="__codelineno-12-24"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-12-25" name="__codelineno-12-25"></a><span class="cm">   * 改</span>
<a id="__codelineno-12-26" name="__codelineno-12-26"></a><span class="cm">   * @param o</span>
<a id="__codelineno-12-27" name="__codelineno-12-27"></a><span class="cm">   * @return</span>
<a id="__codelineno-12-28" name="__codelineno-12-28"></a><span class="cm">   */</span>
<a id="__codelineno-12-29" name="__codelineno-12-29"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">update</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">);</span>
<a id="__codelineno-12-30" name="__codelineno-12-30"></a>
<a id="__codelineno-12-31" name="__codelineno-12-31"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-12-32" name="__codelineno-12-32"></a><span class="cm">   * 查</span>
<a id="__codelineno-12-33" name="__codelineno-12-33"></a><span class="cm">   * @param no</span>
<a id="__codelineno-12-34" name="__codelineno-12-34"></a><span class="cm">   * @return</span>
<a id="__codelineno-12-35" name="__codelineno-12-35"></a><span class="cm">   */</span>
<a id="__codelineno-12-36" name="__codelineno-12-36"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">get</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-12-37" name="__codelineno-12-37"></a>
<a id="__codelineno-12-38" name="__codelineno-12-38"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-12-39" name="__codelineno-12-39"></a><span class="cm">   * 查</span>
<a id="__codelineno-12-40" name="__codelineno-12-40"></a><span class="cm">   * @return</span>
<a id="__codelineno-12-41" name="__codelineno-12-41"></a><span class="cm">   */</span>
<a id="__codelineno-12-42" name="__codelineno-12-42"></a><span class="w">  </span><span class="n">Result</span><span class="w"> </span><span class="nf">getAll</span><span class="p">();</span>
<a id="__codelineno-12-43" name="__codelineno-12-43"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/src/main/java/xust/stu/demo/service/StudentServiceImpl.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-13-1">  1</a></span>
<span class="normal"><a href="#__codelineno-13-2">  2</a></span>
<span class="normal"><a href="#__codelineno-13-3">  3</a></span>
<span class="normal"><a href="#__codelineno-13-4">  4</a></span>
<span class="normal"><a href="#__codelineno-13-5">  5</a></span>
<span class="normal"><a href="#__codelineno-13-6">  6</a></span>
<span class="normal"><a href="#__codelineno-13-7">  7</a></span>
<span class="normal"><a href="#__codelineno-13-8">  8</a></span>
<span class="normal"><a href="#__codelineno-13-9">  9</a></span>
<span class="normal"><a href="#__codelineno-13-10"> 10</a></span>
<span class="normal"><a href="#__codelineno-13-11"> 11</a></span>
<span class="normal"><a href="#__codelineno-13-12"> 12</a></span>
<span class="normal"><a href="#__codelineno-13-13"> 13</a></span>
<span class="normal"><a href="#__codelineno-13-14"> 14</a></span>
<span class="normal"><a href="#__codelineno-13-15"> 15</a></span>
<span class="normal"><a href="#__codelineno-13-16"> 16</a></span>
<span class="normal"><a href="#__codelineno-13-17"> 17</a></span>
<span class="normal"><a href="#__codelineno-13-18"> 18</a></span>
<span class="normal"><a href="#__codelineno-13-19"> 19</a></span>
<span class="normal"><a href="#__codelineno-13-20"> 20</a></span>
<span class="normal"><a href="#__codelineno-13-21"> 21</a></span>
<span class="normal"><a href="#__codelineno-13-22"> 22</a></span>
<span class="normal"><a href="#__codelineno-13-23"> 23</a></span>
<span class="normal"><a href="#__codelineno-13-24"> 24</a></span>
<span class="normal"><a href="#__codelineno-13-25"> 25</a></span>
<span class="normal"><a href="#__codelineno-13-26"> 26</a></span>
<span class="normal"><a href="#__codelineno-13-27"> 27</a></span>
<span class="normal"><a href="#__codelineno-13-28"> 28</a></span>
<span class="normal"><a href="#__codelineno-13-29"> 29</a></span>
<span class="normal"><a href="#__codelineno-13-30"> 30</a></span>
<span class="normal"><a href="#__codelineno-13-31"> 31</a></span>
<span class="normal"><a href="#__codelineno-13-32"> 32</a></span>
<span class="normal"><a href="#__codelineno-13-33"> 33</a></span>
<span class="normal"><a href="#__codelineno-13-34"> 34</a></span>
<span class="normal"><a href="#__codelineno-13-35"> 35</a></span>
<span class="normal"><a href="#__codelineno-13-36"> 36</a></span>
<span class="normal"><a href="#__codelineno-13-37"> 37</a></span>
<span class="normal"><a href="#__codelineno-13-38"> 38</a></span>
<span class="normal"><a href="#__codelineno-13-39"> 39</a></span>
<span class="normal"><a href="#__codelineno-13-40"> 40</a></span>
<span class="normal"><a href="#__codelineno-13-41"> 41</a></span>
<span class="normal"><a href="#__codelineno-13-42"> 42</a></span>
<span class="normal"><a href="#__codelineno-13-43"> 43</a></span>
<span class="normal"><a href="#__codelineno-13-44"> 44</a></span>
<span class="normal"><a href="#__codelineno-13-45"> 45</a></span>
<span class="normal"><a href="#__codelineno-13-46"> 46</a></span>
<span class="normal"><a href="#__codelineno-13-47"> 47</a></span>
<span class="normal"><a href="#__codelineno-13-48"> 48</a></span>
<span class="normal"><a href="#__codelineno-13-49"> 49</a></span>
<span class="normal"><a href="#__codelineno-13-50"> 50</a></span>
<span class="normal"><a href="#__codelineno-13-51"> 51</a></span>
<span class="normal"><a href="#__codelineno-13-52"> 52</a></span>
<span class="normal"><a href="#__codelineno-13-53"> 53</a></span>
<span class="normal"><a href="#__codelineno-13-54"> 54</a></span>
<span class="normal"><a href="#__codelineno-13-55"> 55</a></span>
<span class="normal"><a href="#__codelineno-13-56"> 56</a></span>
<span class="normal"><a href="#__codelineno-13-57"> 57</a></span>
<span class="normal"><a href="#__codelineno-13-58"> 58</a></span>
<span class="normal"><a href="#__codelineno-13-59"> 59</a></span>
<span class="normal"><a href="#__codelineno-13-60"> 60</a></span>
<span class="normal"><a href="#__codelineno-13-61"> 61</a></span>
<span class="normal"><a href="#__codelineno-13-62"> 62</a></span>
<span class="normal"><a href="#__codelineno-13-63"> 63</a></span>
<span class="normal"><a href="#__codelineno-13-64"> 64</a></span>
<span class="normal"><a href="#__codelineno-13-65"> 65</a></span>
<span class="normal"><a href="#__codelineno-13-66"> 66</a></span>
<span class="normal"><a href="#__codelineno-13-67"> 67</a></span>
<span class="normal"><a href="#__codelineno-13-68"> 68</a></span>
<span class="normal"><a href="#__codelineno-13-69"> 69</a></span>
<span class="normal"><a href="#__codelineno-13-70"> 70</a></span>
<span class="normal"><a href="#__codelineno-13-71"> 71</a></span>
<span class="normal"><a href="#__codelineno-13-72"> 72</a></span>
<span class="normal"><a href="#__codelineno-13-73"> 73</a></span>
<span class="normal"><a href="#__codelineno-13-74"> 74</a></span>
<span class="normal"><a href="#__codelineno-13-75"> 75</a></span>
<span class="normal"><a href="#__codelineno-13-76"> 76</a></span>
<span class="normal"><a href="#__codelineno-13-77"> 77</a></span>
<span class="normal"><a href="#__codelineno-13-78"> 78</a></span>
<span class="normal"><a href="#__codelineno-13-79"> 79</a></span>
<span class="normal"><a href="#__codelineno-13-80"> 80</a></span>
<span class="normal"><a href="#__codelineno-13-81"> 81</a></span>
<span class="normal"><a href="#__codelineno-13-82"> 82</a></span>
<span class="normal"><a href="#__codelineno-13-83"> 83</a></span>
<span class="normal"><a href="#__codelineno-13-84"> 84</a></span>
<span class="normal"><a href="#__codelineno-13-85"> 85</a></span>
<span class="normal"><a href="#__codelineno-13-86"> 86</a></span>
<span class="normal"><a href="#__codelineno-13-87"> 87</a></span>
<span class="normal"><a href="#__codelineno-13-88"> 88</a></span>
<span class="normal"><a href="#__codelineno-13-89"> 89</a></span>
<span class="normal"><a href="#__codelineno-13-90"> 90</a></span>
<span class="normal"><a href="#__codelineno-13-91"> 91</a></span>
<span class="normal"><a href="#__codelineno-13-92"> 92</a></span>
<span class="normal"><a href="#__codelineno-13-93"> 93</a></span>
<span class="normal"><a href="#__codelineno-13-94"> 94</a></span>
<span class="normal"><a href="#__codelineno-13-95"> 95</a></span>
<span class="normal"><a href="#__codelineno-13-96"> 96</a></span>
<span class="normal"><a href="#__codelineno-13-97"> 97</a></span>
<span class="normal"><a href="#__codelineno-13-98"> 98</a></span>
<span class="normal"><a href="#__codelineno-13-99"> 99</a></span>
<span class="normal"><a href="#__codelineno-13-100">100</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-13-1" name="__codelineno-13-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu.demo.service</span><span class="p">;</span>
<a id="__codelineno-13-2" name="__codelineno-13-2"></a>
<a id="__codelineno-13-3" name="__codelineno-13-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.Result</span><span class="p">;</span>
<a id="__codelineno-13-4" name="__codelineno-13-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.domain.Student</span><span class="p">;</span>
<a id="__codelineno-13-5" name="__codelineno-13-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.dao.StudentDao</span><span class="p">;</span>
<a id="__codelineno-13-6" name="__codelineno-13-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.dao.StudentDaoImpl</span><span class="p">;</span>
<a id="__codelineno-13-7" name="__codelineno-13-7"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletRequest</span><span class="p">;</span>
<a id="__codelineno-13-8" name="__codelineno-13-8"></a>
<a id="__codelineno-13-9" name="__codelineno-13-9"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">StudentServiceImpl</span><span class="w"> </span><span class="kd">implements</span><span class="w"> </span><span class="n">StudentService</span><span class="p">{</span>
<a id="__codelineno-13-10" name="__codelineno-13-10"></a><span class="w">  </span><span class="kd">private</span><span class="w"> </span><span class="n">StudentDao</span><span class="w"> </span><span class="n">_StudentDao</span><span class="p">;</span>
<a id="__codelineno-13-11" name="__codelineno-13-11"></a>
<a id="__codelineno-13-12" name="__codelineno-13-12"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="nf">StudentServiceImpl</span><span class="p">(){</span>
<a id="__codelineno-13-13" name="__codelineno-13-13"></a><span class="w">    </span><span class="n">_StudentDao</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">StudentDaoImpl</span><span class="p">();</span>
<a id="__codelineno-13-14" name="__codelineno-13-14"></a><span class="w">    </span><span class="n">_StudentDao</span><span class="p">.</span><span class="na">init</span><span class="p">();</span>
<a id="__codelineno-13-15" name="__codelineno-13-15"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-13-16" name="__codelineno-13-16"></a>
<a id="__codelineno-13-17" name="__codelineno-13-17"></a><span class="w">  </span><span class="cm">/**</span>
<a id="__codelineno-13-18" name="__codelineno-13-18"></a><span class="cm">   * 参数填充到对象</span>
<a id="__codelineno-13-19" name="__codelineno-13-19"></a><span class="cm">   */</span>
<a id="__codelineno-13-20" name="__codelineno-13-20"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Student</span><span class="w"> </span><span class="nf">toO</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">){</span>
<a id="__codelineno-13-21" name="__codelineno-13-21"></a><span class="w">    </span><span class="n">Student</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-13-22" name="__codelineno-13-22"></a>
<a id="__codelineno-13-23" name="__codelineno-13-23"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">request</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-24" name="__codelineno-13-24"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Student</span><span class="p">();</span>
<a id="__codelineno-13-25" name="__codelineno-13-25"></a><span class="w">      </span><span class="n">String</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">null</span><span class="p">;</span>
<a id="__codelineno-13-26" name="__codelineno-13-26"></a><span class="w">      </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;no&quot;</span><span class="p">);</span>
<a id="__codelineno-13-27" name="__codelineno-13-27"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-28" name="__codelineno-13-28"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">setNo</span><span class="p">(</span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;no&quot;</span><span class="p">));</span>
<a id="__codelineno-13-29" name="__codelineno-13-29"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-13-30" name="__codelineno-13-30"></a><span class="w">      </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">);</span>
<a id="__codelineno-13-31" name="__codelineno-13-31"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-32" name="__codelineno-13-32"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">setName</span><span class="p">(</span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">));</span>
<a id="__codelineno-13-33" name="__codelineno-13-33"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-13-34" name="__codelineno-13-34"></a><span class="w">      </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;gender&quot;</span><span class="p">);</span>
<a id="__codelineno-13-35" name="__codelineno-13-35"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-36" name="__codelineno-13-36"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">setGender</span><span class="p">(</span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;gender&quot;</span><span class="p">));</span>
<a id="__codelineno-13-37" name="__codelineno-13-37"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-13-38" name="__codelineno-13-38"></a><span class="w">      </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;age&quot;</span><span class="p">);</span>
<a id="__codelineno-13-39" name="__codelineno-13-39"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-40" name="__codelineno-13-40"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">setAge</span><span class="p">(</span><span class="n">Integer</span><span class="p">.</span><span class="na">parseInt</span><span class="p">(</span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;age&quot;</span><span class="p">)));</span>
<a id="__codelineno-13-41" name="__codelineno-13-41"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-13-42" name="__codelineno-13-42"></a><span class="w">      </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;dept&quot;</span><span class="p">);</span>
<a id="__codelineno-13-43" name="__codelineno-13-43"></a><span class="w">      </span><span class="k">if</span><span class="p">(</span><span class="n">p</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-44" name="__codelineno-13-44"></a><span class="w">        </span><span class="n">res</span><span class="p">.</span><span class="na">setDept</span><span class="p">(</span><span class="n">request</span><span class="p">.</span><span class="na">getParameter</span><span class="p">(</span><span class="s">&quot;dept&quot;</span><span class="p">));</span>
<a id="__codelineno-13-45" name="__codelineno-13-45"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-13-46" name="__codelineno-13-46"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-13-47" name="__codelineno-13-47"></a>
<a id="__codelineno-13-48" name="__codelineno-13-48"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-13-49" name="__codelineno-13-49"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-13-50" name="__codelineno-13-50"></a>
<a id="__codelineno-13-51" name="__codelineno-13-51"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">create</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-52" name="__codelineno-13-52"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-13-53" name="__codelineno-13-53"></a>
<a id="__codelineno-13-54" name="__codelineno-13-54"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">_StudentDao</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-55" name="__codelineno-13-55"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_StudentDao</span><span class="p">.</span><span class="na">create</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
<a id="__codelineno-13-56" name="__codelineno-13-56"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-13-57" name="__codelineno-13-57"></a>
<a id="__codelineno-13-58" name="__codelineno-13-58"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-13-59" name="__codelineno-13-59"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-13-60" name="__codelineno-13-60"></a>
<a id="__codelineno-13-61" name="__codelineno-13-61"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">delete</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-62" name="__codelineno-13-62"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-13-63" name="__codelineno-13-63"></a>
<a id="__codelineno-13-64" name="__codelineno-13-64"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">_StudentDao</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-65" name="__codelineno-13-65"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_StudentDao</span><span class="p">.</span><span class="na">delete</span><span class="p">(</span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-13-66" name="__codelineno-13-66"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-13-67" name="__codelineno-13-67"></a>
<a id="__codelineno-13-68" name="__codelineno-13-68"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-13-69" name="__codelineno-13-69"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-13-70" name="__codelineno-13-70"></a>
<a id="__codelineno-13-71" name="__codelineno-13-71"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">update</span><span class="p">(</span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-72" name="__codelineno-13-72"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-13-73" name="__codelineno-13-73"></a>
<a id="__codelineno-13-74" name="__codelineno-13-74"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">_StudentDao</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-75" name="__codelineno-13-75"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_StudentDao</span><span class="p">.</span><span class="na">update</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
<a id="__codelineno-13-76" name="__codelineno-13-76"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-13-77" name="__codelineno-13-77"></a>
<a id="__codelineno-13-78" name="__codelineno-13-78"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-13-79" name="__codelineno-13-79"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-13-80" name="__codelineno-13-80"></a>
<a id="__codelineno-13-81" name="__codelineno-13-81"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">get</span><span class="p">(</span><span class="n">String</span><span class="w"> </span><span class="n">no</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-82" name="__codelineno-13-82"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-13-83" name="__codelineno-13-83"></a>
<a id="__codelineno-13-84" name="__codelineno-13-84"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">_StudentDao</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-85" name="__codelineno-13-85"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_StudentDao</span><span class="p">.</span><span class="na">get</span><span class="p">(</span><span class="n">no</span><span class="p">);</span>
<a id="__codelineno-13-86" name="__codelineno-13-86"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-13-87" name="__codelineno-13-87"></a>
<a id="__codelineno-13-88" name="__codelineno-13-88"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-13-89" name="__codelineno-13-89"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-13-90" name="__codelineno-13-90"></a>
<a id="__codelineno-13-91" name="__codelineno-13-91"></a><span class="w">  </span><span class="kd">public</span><span class="w"> </span><span class="n">Result</span><span class="w"> </span><span class="nf">getAll</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-13-92" name="__codelineno-13-92"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Result</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
<a id="__codelineno-13-93" name="__codelineno-13-93"></a>
<a id="__codelineno-13-94" name="__codelineno-13-94"></a><span class="w">    </span><span class="k">if</span><span class="p">(</span><span class="n">_StudentDao</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="kc">null</span><span class="p">){</span>
<a id="__codelineno-13-95" name="__codelineno-13-95"></a><span class="w">      </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_StudentDao</span><span class="p">.</span><span class="na">getAll</span><span class="p">();</span>
<a id="__codelineno-13-96" name="__codelineno-13-96"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-13-97" name="__codelineno-13-97"></a>
<a id="__codelineno-13-98" name="__codelineno-13-98"></a><span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">res</span><span class="p">;</span>
<a id="__codelineno-13-99" name="__codelineno-13-99"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-13-100" name="__codelineno-13-100"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>创建控制Servlet`StudentAddServlet.java，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTest/src/main/java/xust/stu/demo/action/StudentAddServlet.java</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-14-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-14-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-14-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-14-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-14-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-14-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-14-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-14-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-14-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-14-10">10</a></span>
<span class="normal"><a href="#__codelineno-14-11">11</a></span>
<span class="normal"><a href="#__codelineno-14-12">12</a></span>
<span class="normal"><a href="#__codelineno-14-13">13</a></span>
<span class="normal"><a href="#__codelineno-14-14">14</a></span>
<span class="normal"><a href="#__codelineno-14-15">15</a></span>
<span class="normal"><a href="#__codelineno-14-16">16</a></span>
<span class="normal"><a href="#__codelineno-14-17">17</a></span>
<span class="normal"><a href="#__codelineno-14-18">18</a></span>
<span class="normal"><a href="#__codelineno-14-19">19</a></span>
<span class="normal"><a href="#__codelineno-14-20">20</a></span>
<span class="normal"><a href="#__codelineno-14-21">21</a></span>
<span class="normal"><a href="#__codelineno-14-22">22</a></span>
<span class="normal"><a href="#__codelineno-14-23">23</a></span>
<span class="normal"><a href="#__codelineno-14-24">24</a></span>
<span class="normal"><a href="#__codelineno-14-25">25</a></span>
<span class="normal"><a href="#__codelineno-14-26">26</a></span>
<span class="normal"><a href="#__codelineno-14-27">27</a></span>
<span class="normal"><a href="#__codelineno-14-28">28</a></span>
<span class="normal"><a href="#__codelineno-14-29">29</a></span>
<span class="normal"><a href="#__codelineno-14-30">30</a></span>
<span class="normal"><a href="#__codelineno-14-31">31</a></span>
<span class="normal"><a href="#__codelineno-14-32">32</a></span>
<span class="normal"><a href="#__codelineno-14-33">33</a></span>
<span class="normal"><a href="#__codelineno-14-34">34</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1"></a><span class="kn">package</span><span class="w"> </span><span class="nn">xust.stu.demo.action</span><span class="p">;</span>
<a id="__codelineno-14-2" name="__codelineno-14-2"></a>
<a id="__codelineno-14-3" name="__codelineno-14-3"></a><span class="kn">import</span><span class="w"> </span><span class="nn">java.io.IOException</span><span class="p">;</span>
<a id="__codelineno-14-4" name="__codelineno-14-4"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletConfig</span><span class="p">;</span>
<a id="__codelineno-14-5" name="__codelineno-14-5"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.ServletException</span><span class="p">;</span>
<a id="__codelineno-14-6" name="__codelineno-14-6"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.annotation.WebServlet</span><span class="p">;</span>
<a id="__codelineno-14-7" name="__codelineno-14-7"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServlet</span><span class="p">;</span>
<a id="__codelineno-14-8" name="__codelineno-14-8"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletRequest</span><span class="p">;</span>
<a id="__codelineno-14-9" name="__codelineno-14-9"></a><span class="kn">import</span><span class="w"> </span><span class="nn">javax.servlet.http.HttpServletResponse</span><span class="p">;</span>
<a id="__codelineno-14-10" name="__codelineno-14-10"></a>
<a id="__codelineno-14-11" name="__codelineno-14-11"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.Result</span><span class="p">;</span>
<a id="__codelineno-14-12" name="__codelineno-14-12"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.domain.Student</span><span class="p">;</span>
<a id="__codelineno-14-13" name="__codelineno-14-13"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.service.StudentService</span><span class="p">;</span>
<a id="__codelineno-14-14" name="__codelineno-14-14"></a><span class="kn">import</span><span class="w"> </span><span class="nn">xust.stu.demo.service.StudentServiceImpl</span><span class="p">;</span>
<a id="__codelineno-14-15" name="__codelineno-14-15"></a>
<a id="__codelineno-14-16" name="__codelineno-14-16"></a><span class="nd">@WebServlet</span><span class="p">(</span><span class="s">&quot;/demo/Student/Add&quot;</span><span class="p">)</span>
<a id="__codelineno-14-17" name="__codelineno-14-17"></a><span class="kd">public</span><span class="w"> </span><span class="kd">class</span> <span class="nc">StudentAddServlet</span><span class="w"> </span><span class="kd">extends</span><span class="w"> </span><span class="n">HttpServlet</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-18" name="__codelineno-14-18"></a><span class="w">  </span><span class="kd">private</span><span class="w"> </span><span class="n">StudentService</span><span class="w"> </span><span class="n">_StudentService</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">StudentServiceImpl</span><span class="p">();</span>
<a id="__codelineno-14-19" name="__codelineno-14-19"></a>
<a id="__codelineno-14-20" name="__codelineno-14-20"></a><span class="w">  </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doGet</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span>
<a id="__codelineno-14-21" name="__codelineno-14-21"></a><span class="w">      </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-22" name="__codelineno-14-22"></a>
<a id="__codelineno-14-23" name="__codelineno-14-23"></a><span class="w">    </span><span class="n">Student</span><span class="w"> </span><span class="n">o</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_StudentService</span><span class="p">.</span><span class="na">toO</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
<a id="__codelineno-14-24" name="__codelineno-14-24"></a><span class="w">    </span><span class="n">Result</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_StudentService</span><span class="p">.</span><span class="na">create</span><span class="p">(</span><span class="n">o</span><span class="p">);</span>
<a id="__codelineno-14-25" name="__codelineno-14-25"></a><span class="w">    </span><span class="n">response</span><span class="p">.</span><span class="na">setCharacterEncoding</span><span class="p">(</span><span class="s">&quot;utf-8&quot;</span><span class="p">);</span>
<a id="__codelineno-14-26" name="__codelineno-14-26"></a><span class="w">    </span><span class="n">response</span><span class="p">.</span><span class="na">setContentType</span><span class="p">(</span><span class="s">&quot;application/json&quot;</span><span class="p">);</span>
<a id="__codelineno-14-27" name="__codelineno-14-27"></a><span class="w">    </span><span class="n">response</span><span class="p">.</span><span class="na">getWriter</span><span class="p">().</span><span class="na">append</span><span class="p">(</span><span class="n">String</span><span class="p">.</span><span class="na">format</span><span class="p">(</span><span class="s">&quot;{\&quot;code\&quot;:%d, \&quot;message\&quot;:\&quot;%s\&quot;, \&quot;data\&quot;:\&quot;%s\&quot;}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="na">code</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="na">message</span><span class="p">,</span><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="na">data</span><span class="p">));</span>
<a id="__codelineno-14-28" name="__codelineno-14-28"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-14-29" name="__codelineno-14-29"></a>
<a id="__codelineno-14-30" name="__codelineno-14-30"></a><span class="w">  </span><span class="kd">protected</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">doPost</span><span class="p">(</span><span class="n">HttpServletRequest</span><span class="w"> </span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">HttpServletResponse</span><span class="w"> </span><span class="n">response</span><span class="p">)</span><span class="w"> </span>
<a id="__codelineno-14-31" name="__codelineno-14-31"></a><span class="w">      </span><span class="kd">throws</span><span class="w"> </span><span class="n">ServletException</span><span class="p">,</span><span class="w"> </span><span class="n">IOException</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-14-32" name="__codelineno-14-32"></a><span class="w">    </span><span class="n">doGet</span><span class="p">(</span><span class="n">request</span><span class="p">,</span><span class="w"> </span><span class="n">response</span><span class="p">);</span>
<a id="__codelineno-14-33" name="__codelineno-14-33"></a><span class="w">  </span><span class="p">}</span>
<a id="__codelineno-14-34" name="__codelineno-14-34"></a><span class="p">}</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>编写测试代码(Rest Client)，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">DaoTestResult/StudentServletTest.http</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-15-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-15-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-15-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-15-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-15-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-15-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-15-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-15-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-15-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-15-10">10</a></span>
<span class="normal"><a href="#__codelineno-15-11">11</a></span>
<span class="normal"><a href="#__codelineno-15-12">12</a></span>
<span class="normal"><a href="#__codelineno-15-13">13</a></span>
<span class="normal"><a href="#__codelineno-15-14">14</a></span>
<span class="normal"><a href="#__codelineno-15-15">15</a></span>
<span class="normal"><a href="#__codelineno-15-16">16</a></span>
<span class="normal"><a href="#__codelineno-15-17">17</a></span>
<span class="normal"><a href="#__codelineno-15-18">18</a></span>
<span class="normal"><a href="#__codelineno-15-19">19</a></span>
<span class="normal"><a href="#__codelineno-15-20">20</a></span>
<span class="normal"><a href="#__codelineno-15-21">21</a></span>
<span class="normal"><a href="#__codelineno-15-22">22</a></span>
<span class="normal"><a href="#__codelineno-15-23">23</a></span>
<span class="normal"><a href="#__codelineno-15-24">24</a></span>
<span class="normal"><a href="#__codelineno-15-25">25</a></span>
<span class="normal"><a href="#__codelineno-15-26">26</a></span>
<span class="normal"><a href="#__codelineno-15-27">27</a></span>
<span class="normal"><a href="#__codelineno-15-28">28</a></span>
<span class="normal"><a href="#__codelineno-15-29">29</a></span>
<span class="normal"><a href="#__codelineno-15-30">30</a></span>
<span class="normal"><a href="#__codelineno-15-31">31</a></span>
<span class="normal"><a href="#__codelineno-15-32">32</a></span>
<span class="normal"><a href="#__codelineno-15-33">33</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-15-1" name="__codelineno-15-1"></a><span class="err">@app = DaoTest</span>
<a id="__codelineno-15-2" name="__codelineno-15-2"></a>
<a id="__codelineno-15-3" name="__codelineno-15-3"></a><span class="err">### 增</span>
<a id="__codelineno-15-4" name="__codelineno-15-4"></a><span class="nf">POST</span> <span class="nn">http://localhost:8080/{{app}}/demo/Student/Add</span> <span class="kr">HTTP</span><span class="o">/</span><span class="m">1.1</span>
<a id="__codelineno-15-5" name="__codelineno-15-5"></a><span class="na">Content-Type</span><span class="o">:</span> <span class="l">application/x-www-form-urlencoded</span>
<a id="__codelineno-15-6" name="__codelineno-15-6"></a>
<a id="__codelineno-15-7" name="__codelineno-15-7"></a><span class="nt">no</span><span class="o">=</span><span class="s">X2</span>
<a id="__codelineno-15-8" name="__codelineno-15-8"></a><span class="p">&amp;</span><span class="nt">name</span><span class="o">=</span><span class="s">X2</span>
<a id="__codelineno-15-9" name="__codelineno-15-9"></a><span class="p">&amp;</span><span class="nt">gender</span><span class="o">=</span><span class="s">X2</span>
<a id="__codelineno-15-10" name="__codelineno-15-10"></a><span class="p">&amp;</span><span class="nt">age</span><span class="o">=</span><span class="s">2</span>
<a id="__codelineno-15-11" name="__codelineno-15-11"></a><span class="p">&amp;</span><span class="nt">dept</span><span class="o">=</span><span class="s">X2</span>
<a id="__codelineno-15-12" name="__codelineno-15-12"></a>
<a id="__codelineno-15-13" name="__codelineno-15-13"></a><span class="s">### 删</span>
<a id="__codelineno-15-14" name="__codelineno-15-14"></a><span class="s">POST http://localhost:8080/{{app}}/demo/Student/Delete HTTP/1.1</span>
<a id="__codelineno-15-15" name="__codelineno-15-15"></a><span class="s">Content-Type: application/x-www-form-urlencoded</span>
<a id="__codelineno-15-16" name="__codelineno-15-16"></a>
<a id="__codelineno-15-17" name="__codelineno-15-17"></a><span class="s">no</span><span class="o">=</span><span class="s">X2</span>
<a id="__codelineno-15-18" name="__codelineno-15-18"></a>
<a id="__codelineno-15-19" name="__codelineno-15-19"></a><span class="s">### 改</span>
<a id="__codelineno-15-20" name="__codelineno-15-20"></a><span class="s">POST http://localhost:8080/{{app}}/demo/Student/Update HTTP/1.1</span>
<a id="__codelineno-15-21" name="__codelineno-15-21"></a><span class="s">Content-Type: application/x-www-form-urlencoded</span>
<a id="__codelineno-15-22" name="__codelineno-15-22"></a>
<a id="__codelineno-15-23" name="__codelineno-15-23"></a><span class="s">no</span><span class="o">=</span><span class="s">X2</span>
<a id="__codelineno-15-24" name="__codelineno-15-24"></a><span class="p">&amp;</span><span class="nt">name</span><span class="o">=</span><span class="s">3</span>
<a id="__codelineno-15-25" name="__codelineno-15-25"></a><span class="p">&amp;</span><span class="nt">gender</span><span class="o">=</span><span class="s">3</span>
<a id="__codelineno-15-26" name="__codelineno-15-26"></a><span class="p">&amp;</span><span class="nt">age</span><span class="o">=</span><span class="s">3</span>
<a id="__codelineno-15-27" name="__codelineno-15-27"></a><span class="p">&amp;</span><span class="nt">dept</span><span class="o">=</span><span class="s">3</span>
<a id="__codelineno-15-28" name="__codelineno-15-28"></a>
<a id="__codelineno-15-29" name="__codelineno-15-29"></a><span class="s">### 查</span>
<a id="__codelineno-15-30" name="__codelineno-15-30"></a><span class="s">POST http://localhost:8080/{{app}}/demo/Student/Get HTTP/1.1</span>
<a id="__codelineno-15-31" name="__codelineno-15-31"></a><span class="s">Content-Type: application/x-www-form-urlencoded</span>
<a id="__codelineno-15-32" name="__codelineno-15-32"></a>
<a id="__codelineno-15-33" name="__codelineno-15-33"></a><span class="s">no</span><span class="o">=</span><span class="s">X2</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>分别发送CRUD请求，验证代码可以正常工作</p>
</li>
</ol>
<h3 id="_5">拓展练习</h3>
<ol>
<li>使用Servlet实现课程表的增、删、改、查操作</li>
</ol>












                
              </article>
            </div>
          
          
  <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script>

<script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script>
        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    
    <script id="__config" type="application/json">{"base": "../..", "features": ["content.code.copy", "content.tabs.link", "navigation.expand", "navigation.sections", "navigation.path"], "search": "../../assets/javascripts/workers/search.f8cc74c7.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>
    
    
      <script src="../../assets/javascripts/bundle.f1b6f286.min.js"></script>
      
    
  </body>
</html>